From 6969d256ce37efa818d0c58640eda21d16aa243c Mon Sep 17 00:00:00 2001 From: Michal Blaha Date: Sun, 30 Jul 2023 12:00:05 +0200 Subject: [PATCH] Update NativeMethods.cs In Docker (ubuntu/Debian), when .net console app with PDFtoImage is call from another application, workingDirectory is not setup. Assembly.GetExecutingAssembly().Location returns empty string, not null and new Uri(workingDirectory) than failes with exception "empty Uri" --- src/PDFtoImage/PdfiumViewer/NativeMethods.cs | 27 +++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/PDFtoImage/PdfiumViewer/NativeMethods.cs b/src/PDFtoImage/PdfiumViewer/NativeMethods.cs index 04f2dc7e..bee78e68 100644 --- a/src/PDFtoImage/PdfiumViewer/NativeMethods.cs +++ b/src/PDFtoImage/PdfiumViewer/NativeMethods.cs @@ -12,20 +12,23 @@ static NativeMethods() { // Load the platform dependent Pdfium.dll if it exist. #if NET7_0_OR_GREATER - var workingDirectory = - Assembly.GetExecutingAssembly().Location - ?? Environment.ProcessPath! - ?? AppContext.BaseDirectory; + string workingDirectory = Assembly.GetExecutingAssembly().Location; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = Environment.ProcessPath!; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = AppContext.BaseDirectory; #elif NET6_0_OR_GREATER - var workingDirectory = - Assembly.GetExecutingAssembly().GetName(false).CodeBase - ?? Environment.ProcessPath! - ?? AppContext.BaseDirectory; + var workingDirectory = Assembly.GetExecutingAssembly().GetName(false).CodeBase; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = Environment.ProcessPath!; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = AppContext.BaseDirectory; #else - var workingDirectory = - Assembly.GetExecutingAssembly().GetName(false).CodeBase - ?? Process.GetCurrentProcess().MainModule!.FileName! - ?? AppContext.BaseDirectory; + var workingDirectory =Assembly.GetExecutingAssembly().GetName(false).CodeBase; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = Process.GetCurrentProcess().MainModule!.FileName!; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = AppContext.BaseDirectory; #endif LoadNativeLibrary(Path.GetDirectoryName(new Uri(workingDirectory).LocalPath)!);