Browse Source

Entry assembly can be null (e.g. when running unit tests)

Source commit: 97f6270d37f1b1bbddf89b1e4bbb5fd607092bd9
Martin Prikryl 6 years ago
parent
commit
70a1459b31
2 changed files with 13 additions and 4 deletions
  1. 8 2
      dotnet/internal/ExeSessionProcess.cs
  2. 5 2
      dotnet/internal/Logger.cs

+ 8 - 2
dotnet/internal/ExeSessionProcess.cs

@@ -849,11 +849,17 @@ namespace WinSCP
 #endif
                         !TryFindExecutableInPath(GetDefaultInstallationPath(), out executablePath))
                     {
+                        string entryAssemblyDesc = string.Empty;
+                        Assembly entryAssembly = Assembly.GetEntryAssembly();
+                        if (entryAssembly != null)
+                        {
+                            entryAssemblyDesc = $", nor the entry assembly {entryAssembly.GetName().Name} ({GetEntryAssemblyPath()})";
+                        }
                         throw _logger.WriteException(
                             new SessionLocalException(_session,
                                 string.Format(CultureInfo.CurrentCulture,
-                                    "The {0} executable was not found at location of the assembly {1} ({2}), nor the executing assembly {3} ({4}), nor in an installation path. You may use Session.ExecutablePath property to explicitly set path to {0}.",
-                                    ExeExecutableFileName, Assembly.GetExecutingAssembly(), GetAssemblyPath(), Assembly.GetEntryAssembly(), GetEntryAssemblyPath())));
+                                    "The {0} executable was not found at location of the assembly {1} ({2}){3}, nor in an installation path. You may use Session.ExecutablePath property to explicitly set path to {0}.",
+                                    ExeExecutableFileName, Assembly.GetExecutingAssembly().GetName().Name, GetAssemblyPath(), entryAssemblyDesc)));
                     }
                 }
                 return executablePath;

+ 5 - 2
dotnet/internal/Logger.cs

@@ -25,7 +25,7 @@ namespace WinSCP
         public string GetEntryAssemblyFilePath()
         {
             Assembly assembly = Assembly.GetEntryAssembly();
-            return DoGetAssemblyFilePath(assembly);
+            return (assembly != null) ? DoGetAssemblyFilePath(assembly) : null;
         }
 
         private string DoGetAssemblyFilePath(Assembly assembly)
@@ -380,7 +380,10 @@ namespace WinSCP
             FileVersionInfo version = string.IsNullOrEmpty(path) ? null : FileVersionInfo.GetVersionInfo(path);
             WriteLine("Assembly path: {0}", path);
             WriteLine("Assembly product version: {0}", ((version != null) ? version.ProductVersion : "unknown"));
-            WriteLine("Entry assembly path: {0}", GetEntryAssemblyFilePath());
+            if (Assembly.GetEntryAssembly() != null)
+            {
+                WriteLine("Entry assembly path: {0}", GetEntryAssemblyFilePath());
+            }
         }
 
         public static string LastWin32ErrorMessage()