Example #1
0
        public static void DefaultContextOverrideTPA()
        {
            var assemblyNameStr = "System.Runtime.Loader.Noop.Assembly.dll";
            var lcDefault       = AssemblyLoadContext.Default;

            // Load the assembly in custom load context
            OverrideDefaultLoadContext olc = new OverrideDefaultLoadContext();
            var asmTargetAsm  = olc.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, olc);
            Assert.Equal(olc, 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", true);
            var  method = System.Reflection.TypeExtensions.GetMethod(type, "LoadFromDefaultContext");

            // Load System.Runtime - since this is on TPA, it should get resolved from our custom load context
            // since the Load method has been implemented to override TPA assemblies.
            var assemblyName = "System.Runtime, Version=4.0.0.0";

            olc.LoadedFromContext = false;
            Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] { assemblyName });

            loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded);

            // Confirm assembly did not load from DefaultContext
            Assert.NotEqual(lcDefault, loadedContext);
            Assert.Equal(olc, loadedContext);
            Assert.Equal(true, olc.LoadedFromContext);

            // Now, do the same for an assembly that we explicitly had loaded in DefaultContext
            // in the caller of this method and ALSO loaded in the current load context. We should get it from our LoadContext,
            // without invoking the Load override, since it is already loaded.
            assemblyName          = "System.Runtime.Loader.Noop.Assembly";
            olc.LoadedFromContext = false;
            Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] { assemblyName });

            loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2);

            // Confirm assembly loaded from the intended LoadContext
            Assert.NotEqual(lcDefault, loadedContext);
            Assert.Equal(olc, loadedContext);
            Assert.Equal(false, olc.LoadedFromContext);
        }
        public static void DefaultContextOverrideTPA()
        {
            var assemblyNameStr = "System.Runtime.Loader.Noop.Assembly.dll";
            var lcDefault = AssemblyLoadContext.Default;
            
            // Load the assembly in custom load context
            OverrideDefaultLoadContext olc = new OverrideDefaultLoadContext();
            var asmTargetAsm = olc.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, olc);
            Assert.Equal(olc, 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 our custom load context
            // since the Load method has been implemented to override TPA assemblies.
            var assemblyName = "System.Runtime, Version=4.0.0.0";
            olc.LoadedFromContext = false;
            Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] {assemblyName});
            loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded);

            // Confirm assembly did not load from DefaultContext
            Assert.NotEqual(lcDefault, loadedContext);
            Assert.Equal(olc, loadedContext);
            Assert.Equal(true, olc.LoadedFromContext);

            // Now, do the same from an assembly that we explicitly had loaded in DefaultContext
            // in the caller of this method and ALSO loaded in the current load context. We should get it from our LoadContext,
            // without invoking the Load override, since it is already loaded.
            assemblyName = "System.Runtime.Loader.Noop.Assembly";
            olc.LoadedFromContext = false;
            Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] {assemblyName});
            loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2);

            // Confirm assembly loaded from the intended LoadContext
            Assert.NotEqual(lcDefault, loadedContext);
            Assert.Equal(olc, loadedContext);
            Assert.Equal(false, olc.LoadedFromContext);
        }