public static void DefaultContextFallback() { var assemblyNameStr = "System.Runtime.Loader.Noop.Assembly.dll"; var lcDefault = AssemblyLoadContext.Default; // Load the assembly in custom load context FallbackLoadContext flc = new FallbackLoadContext(); var asmTargetAsm = flc.LoadFromAssemblyPath(Path.Combine(s_loadFromPath, assemblyNameStr)); var loadedContext = AssemblyLoadContext.GetLoadContext(asmTargetAsm); // LoadContext of the assembly should be the custom context and not DefaultContext Assert.NotEqual(lcDefault, flc); Assert.Equal(flc, loadedContext); // Get reference to the helper method that will load assemblies (actually, resolve them) // from DefaultContext Type type = asmTargetAsm.GetType("System.Runtime.Loader.Tests.TestClass"); var method = System.Reflection.TypeExtensions.GetMethod(type, "LoadFromDefaultContext"); // Load System.Runtime - since this is on TPA, it should get resolved from DefaultContext // since FallbackLoadContext does not override the Load method to specify its location. var assemblyName = "System.Runtime, Version=4.0.0.0"; Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] { assemblyName }); loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded); // Confirm assembly Loaded from DefaultContext Assert.Equal(lcDefault, loadedContext); Assert.NotEqual(flc, loadedContext); // Now, do the same from an assembly that we explicitly had loaded in DefaultContext // in the caller of this method. We should get it from FallbackLoadContext since we // explicitly loaded it there as well. assemblyName = "System.Runtime.Loader.Noop.Assembly"; Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] { assemblyName }); loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2); Assert.NotEqual(lcDefault, loadedContext); Assert.Equal(flc, loadedContext); // Attempt to bind an assembly that has not been loaded in DefaultContext and is not // present on TPA as well. Such an assembly will not trigger a load since we only consult // the DefaultContext cache (including assemblies on TPA list) in an attempt to bind. assemblyName = "System.Runtime.Loader.Noop.Assembly.NonExistent"; Exception ex = null; try { method.Invoke(null, new object[] { assemblyName }); } catch (TargetInvocationException tie) { ex = tie.InnerException; } Assert.Equal(typeof(FileNotFoundException), ex.GetType()); }
public static void DefaultContextFallback() { var assemblyNameStr = "System.Runtime.Loader.Noop.Assembly.dll"; var lcDefault = AssemblyLoadContext.Default; // Load the assembly in custom load context FallbackLoadContext flc = new FallbackLoadContext(); var asmTargetAsm = flc.LoadFromAssemblyPath(Path.Combine(s_loadFromPath, assemblyNameStr)); var loadedContext = AssemblyLoadContext.GetLoadContext(asmTargetAsm); // LoadContext of the assembly should be the custom context and not DefaultContext Assert.NotEqual(lcDefault, flc); Assert.Equal(flc, loadedContext); // Get reference to the helper method that will load assemblies (actually, resolve them) // from DefaultContext Type type = asmTargetAsm.GetType("System.Runtime.Loader.Tests.TestClass"); var method = System.Reflection.TypeExtensions.GetMethod(type, "LoadFromDefaultContext"); // Load System.Runtime - since this is on TPA, it should get resolved from DefaultContext // since FallbackLoadContext does not override the Load method to specify its location. var assemblyName = "System.Runtime, Version=4.0.0.0"; Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] {assemblyName}); loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded); // Confirm assembly Loaded from DefaultContext Assert.Equal(lcDefault, loadedContext); Assert.NotEqual(flc, loadedContext); // Now, do the same from an assembly that we explicitly had loaded in DefaultContext // in the caller of this method. We should get it from FallbackLoadContext since we // explicitly loaded it there as well. assemblyName = "System.Runtime.Loader.Noop.Assembly"; Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] {assemblyName}); loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2); Assert.NotEqual(lcDefault, loadedContext); Assert.Equal(flc, loadedContext); // Attempt to bind an assembly that has not been loaded in DefaultContext and is not // present on TPA as well. Such an assembly will not trigger a load since we only consult // the DefaultContext cache (including assemblies on TPA list) in an attempt to bind. assemblyName = "System.Runtime.Loader.Noop.Assembly.NonExistent"; Exception ex = null; try { method.Invoke(null, new object[] {assemblyName}); } catch(TargetInvocationException tie) { ex = tie.InnerException; } Assert.Equal(typeof(FileNotFoundException), ex.GetType()); }