From 839014b5992028a7211c3629ec434f10a9f190fd Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 8 Apr 2016 19:02:29 -0700 Subject: [PATCH] Try Assembly.Load correctly This should resolve #791. Instead of checking if `Assembly.Load` returned null (it doesn't), check if it threw `FileNotFoundException`, which it will if it doesn't find the assembly. --- .../CoreCLR/CorePsAssemblyLoadContext.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs index 9225655743..8c17f5e860 100644 --- a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs @@ -186,10 +186,12 @@ namespace System.Management.Automation // Try loading it from the TPA list. All PowerShell dependencies are in the // TPA list when published with dotnet-cli. - asmLoaded = Assembly.Load(assemblyName); - + try + { + asmLoaded = Assembly.Load(assemblyName); + } // If it wasn't there, try loading from path - if (asmLoaded == null) + catch (FileNotFoundException) { asmLoaded = asmFilePath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase) ? LoadFromNativeImagePath(asmFilePath, null) @@ -250,9 +252,12 @@ namespace System.Management.Automation // Try loading it from the TPA list. All PowerShell dependencies are in the // TPA list when published with dotnet-cli. - asmLoaded = Assembly.Load(assemblyName); - - if (asmLoaded == null) + try + { + asmLoaded = Assembly.Load(assemblyName); + } + // If it wasn't there, try loading from path + catch (FileNotFoundException) { // Load the assembly through 'LoadFromNativeImagePath' or 'LoadFromAssemblyPath' asmLoaded = assemblyPath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase) @@ -520,4 +525,4 @@ namespace System.Management.Automation } } -#endif \ No newline at end of file +#endif