public static void DefaultName()
    {
        try
        {
            Console.WriteLine("DefaultName()");

            AssemblyLoadContext alc = AssemblyLoadContext.Default;
            Assert(alc == AssemblyLoadContext.GetLoadContext(typeof(Program).Assembly));
            Assert(PropertyAllContainsContext(alc));
            Assert(AssembliesContainAssembly(alc, typeof(Program).Assembly));

            Console.WriteLine(alc.Name);
            Assert(alc.Name == "Default");

            Console.WriteLine(alc.GetType().ToString());
            Assert(alc.GetType().ToString() == "System.Runtime.Loader.DefaultAssemblyLoadContext");

            Console.WriteLine(alc.ToString());
            Assert(alc.ToString().Contains("\"Default"));
            Assert(alc.ToString().Contains("\" System.Runtime.Loader.DefaultAssemblyLoadContext"));
            Assert(alc.ToString().Contains(" #"));
        }
        catch (Exception e)
        {
            Assert(false, e.ToString());
        }
    }
    public static void AssemblyLoadByteArrayName()
    {
#if runDisabled // This test case fails when the assembly is a ready2run image
        try
        {
            Console.WriteLine("AssemblyLoadByteArrayName()");

            String              path      = typeof(Program).Assembly.Location;
            Byte []             byteArray = System.IO.File.ReadAllBytes(path);
            Assembly            a         = Assembly.Load(byteArray);
            AssemblyLoadContext alc       = AssemblyLoadContext.GetLoadContext(a);
            Assert(PropertyAllContainsContext(alc));
            Assert(AssembliesContainAssembly(alc, a));

            Console.WriteLine(alc.Name);
            Assert(alc.Name == "Assembly.Load(byte[], ...)");

            Console.WriteLine(alc.GetType().ToString());
            Assert(alc.GetType().ToString() == "System.Runtime.Loader.IndividualAssemblyLoadContext");

            Console.WriteLine(alc.ToString());
            Assert(alc.ToString().Contains("\"Assembly.Load(byte[], ...)\""));
            Assert(alc.ToString().Contains("\" System.Runtime.Loader.IndividualAssemblyLoadContext"));
            Assert(alc.ToString().Contains(" #"));
        }
        catch (Exception e)
        {
            Assert(false, e.ToString());
        }
#endif
    }
    public static void CustomName()
    {
        try
        {
            Console.WriteLine("CustomName()");

            // ALC should be a concrete class
            AssemblyLoadContext alc = new AssemblyLoadContext("CustomName");
            Assert(PropertyAllContainsContext(alc));

            Console.WriteLine(alc.Name);
            Assert(alc.Name == "CustomName");

            Console.WriteLine(alc.GetType().ToString());
            Assert(alc.GetType().ToString() == "System.Runtime.Loader.AssemblyLoadContext");

            Console.WriteLine(alc.ToString());
            Assert(alc.ToString().Contains("\"CustomName"));
            Assert(alc.ToString().Contains("\" System.Runtime.Loader.AssemblyLoadContext"));
            Assert(alc.ToString().Contains(" #"));
        }
        catch (Exception e)
        {
            Assert(false, e.ToString());
        }
    }
    public static void AssemblyLoadFileName()
    {
        try
        {
            Console.WriteLine("AssemblyLoadFileName()");

            String   path = typeof(Program).Assembly.Location;
            Assembly a    = Assembly.LoadFile(path);

            Assert(a != typeof(Program).Assembly);

            AssemblyLoadContext alc = AssemblyLoadContext.GetLoadContext(a);
            Assert(PropertyAllContainsContext(alc));
            Assert(AssembliesContainAssembly(alc, a));
            Assert(alc != AssemblyLoadContext.Default);


            Console.WriteLine(alc.Name);
            Assert(alc.Name == String.Format("Assembly.LoadFile({0})", path));

            Console.WriteLine(alc.GetType().ToString());
            Assert(alc.GetType().ToString() == "System.Runtime.Loader.IndividualAssemblyLoadContext");

            Console.WriteLine(alc.ToString());
            Assert(alc.ToString().Contains("\"" + String.Format("Assembly.LoadFile({0})", path)));
            Assert(alc.ToString().Contains("\" System.Runtime.Loader.IndividualAssemblyLoadContext"));
            Assert(alc.ToString().Contains(" #"));
        }
        catch (Exception e)
        {
            Assert(false, e.ToString());
        }
    }
    public static void AssemblyLoadByteArrayName()
    {
#if ReadAllBytes // System.IO.File.ReadAllBytes is not found when ReferenceSystemPrivateCoreLib is true
        try
        {
            Console.WriteLine("AssemblyLoadByteArrayName()");

            String              path      = typeof(Program).Assembly.Location;
            Byte []             byteArray = System.IO.File.ReadAllBytes(path);
            Assembly            a         = Assembly.Load(byteArray);
            AssemblyLoadContext alc       = AssemblyLoadContext.GetLoadContext(a);
            Assert(PropertyAllContainsContext(alc));
            Assert(AssembliesContainAssembly(alc, a));

            Console.WriteLine(alc.Name);
            Assert(alc.Name == "Assembly.Load(byte[], ...)");

            Console.WriteLine(alc.GetType().ToString());
            Assert(alc.GetType().ToString() == "System.Runtime.Loader.IndividualAssemblyLoadContext");

            Console.WriteLine(alc.ToString());
            Assert(alc.ToString().Contains("\"Default"));
            Assert(alc.ToString().Contains("\" System.Runtime.Loader.IndividualAssemblyLoadContext"));
            Assert(alc.ToString().Contains(" #"));
        }
        catch (Exception e)
        {
            Assert(false, e.ToString());
        }
#endif
    }
        public static void PublicConstructor_Theory(string name, bool isCollectible)
        {
            AssemblyLoadContext alc = new AssemblyLoadContext(name, isCollectible);

            Assert.Equal(isCollectible, alc.IsCollectible);

            Assert.Equal(name, alc.Name);
            Assert.Contains(name, alc.ToString());
            Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
            Assert.Contains(alc, AssemblyLoadContext.All);
            Assert.Empty(alc.Assemblies);
        }
        public static void PublicConstructor_Default()
        {
            AssemblyLoadContext alc = new AssemblyLoadContext("PublicConstructor");

            Assert.False(alc.IsCollectible);

            Assert.Equal("PublicConstructor", alc.Name);
            Assert.Contains("PublicConstructor", alc.ToString());
            Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
            Assert.Contains(alc, AssemblyLoadContext.All);
            Assert.Empty(alc.Assemblies);
        }
        public static void DefaultAssemblyLoadContext_Properties()
        {
            AssemblyLoadContext alc = AssemblyLoadContext.Default;

            Assert.False(alc.IsCollectible);

            Assert.Equal("Default", alc.Name);
            Assert.Contains("\"Default\"", alc.ToString());
            Assert.Contains("System.Runtime.Loader.DefaultAssemblyLoadContext", alc.ToString());
            Assert.Contains(alc, AssemblyLoadContext.All);
            Assert.Contains(Assembly.GetCallingAssembly(), alc.Assemblies);
        }
 /// <summary>
 /// Terminating the linux process gracefully <see cref="!:http://www.ben-morris.com/using-docker-to-build-and-deploy-net-core-console-applications/"/>
 /// </summary>
 /// <param name="obj"></param>
 private static void Default_Unloading(AssemblyLoadContext obj)
 {
     // Console.WriteLine($"Shutting down in response to SIGTERM.");
     _logger.LogInformation($"Shutting down in response to SIGTERM. {obj.ToString()}");
     _shutdown.Set();
     _complete.Wait();
 }
Example #10
0
        public void Assembly_IsCollectibleFalse_WhenUsingAssemblyLoad()
        {
            RemoteExecutor.Invoke(() => {
                Assembly asm = Assembly.LoadFrom(asmPath);

                Assert.NotNull(asm);

                Assert.False(asm.IsCollectible);

                AssemblyLoadContext alc = AssemblyLoadContext.GetLoadContext(asm);
                Assert.False(alc.IsCollectible);
                Assert.Equal(AssemblyLoadContext.Default, alc);
                Assert.Equal("Default", alc.Name);
                Assert.Contains("\"Default\"", alc.ToString());
                Assert.Contains("System.Runtime.Loader.DefaultAssemblyLoadContext", alc.ToString());
                Assert.Contains(alc, AssemblyLoadContext.All);
                Assert.Contains(asm, alc.Assemblies);
            }).Dispose();
        }
Example #11
0
        public void Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext()
        {
            RemoteExecutor.Invoke(() => {
                AssemblyLoadContext alc = new AssemblyLoadContext("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext");

                Assembly asm = alc.LoadFromAssemblyPath(asmPath);

                Assert.NotNull(asm);

                Assert.False(asm.IsCollectible);
                Assert.False(alc.IsCollectible);

                Assert.Equal("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.Name);
                Assert.Contains("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.ToString());
                Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
                Assert.Contains(alc, AssemblyLoadContext.All);
                Assert.Contains(asm, alc.Assemblies);
            }).Dispose();
        }
Example #12
0
        public void Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext()
        {
            RemoteInvoke(() => {
                AssemblyLoadContext alc = new AssemblyLoadContext("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext");

                Assembly asm = alc.LoadFromAssemblyPath(asmPath);

                Assert.NotNull(asm);

                Assert.False(asm.IsCollectible);
                Assert.False(alc.IsCollectible);

                Assert.Equal("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.Name);
                Assert.Contains("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.ToString());
                Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
                Assert.Contains(alc, AssemblyLoadContext.All);
#if CoreCLR_23583
                Assert.Contains(asm, alc.Assemblies);
#endif

                return(SuccessExitCode);
            }).Dispose();
        }
 /// <summary>
 /// Plugins the load context_ unloading.
 /// </summary>
 /// <param name="obj">The obj.</param>
 private void PluginsLoadContext_Unloading(AssemblyLoadContext obj)
 {
     Console.WriteLine(obj.ToString());
 }
            private Assembly ResolveAssembly(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                if (handlerReturn == HandlerReturn.Null)
                {
                    return(null);
                }

                string name         = handlerReturn == HandlerReturn.RequestedAssembly ? assemblyName.Name : $"{assemblyName.Name}Mismatch";
                string assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(name);

                if (!File.Exists(assemblyPath))
                {
                    return(null);
                }

                Assembly asm  = context.LoadFromAssemblyPath(assemblyPath);
                var      bind = new BindOperation()
                {
                    AssemblyName                  = asm.GetName(),
                    AssemblyPath                  = assemblyPath,
                    AssemblyLoadContext           = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                    RequestingAssembly            = CoreLibName,
                    RequestingAssemblyLoadContext = DefaultALC,
                    Success            = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached             = false
                };

                Binds.Add(bind);
                return(asm);
            }
            private Assembly OnAssemblyLoadContextResolving(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                Assembly asm        = ResolveAssembly(context, assemblyName);
                var      invocation = new HandlerInvocation()
                {
                    AssemblyName        = assemblyName,
                    HandlerName         = nameof(OnAssemblyLoadContextResolving),
                    AssemblyLoadContext = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
            private Assembly OnAssemblyLoadContextResolving(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                if (handlerReturn == HandlerReturn.Exception)
                {
                    throw new BinderTestException("Exception in handler for AssemblyLoadContext.Resolving");
                }

                Assembly asm        = ResolveAssembly(context, assemblyName);
                var      invocation = new HandlerInvocation()
                {
                    AssemblyName        = assemblyName,
                    HandlerName         = nameof(OnAssemblyLoadContextResolving),
                    AssemblyLoadContext = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
Example #17
0
        private static ResolutionAttempt GetResolutionAttempt(AssemblyName assemblyName, ResolutionStage stage, AssemblyLoadContext alc, ResolutionResult result, AssemblyName resultAssemblyName, string resultAssemblyPath, string errorMessage = null)
        {
            var attempt = new ResolutionAttempt()
            {
                AssemblyName        = assemblyName,
                Stage               = stage,
                AssemblyLoadContext = alc == AssemblyLoadContext.Default ? DefaultALC : alc.ToString(),
                Result              = result,
                ResultAssemblyName  = resultAssemblyName,
                ResultAssemblyPath  = resultAssemblyPath
            };

            if (!string.IsNullOrEmpty(errorMessage))
            {
                attempt.ErrorMessage = errorMessage;
            }
            else
            {
                switch (result)
                {
                case ResolutionAttempt.ResolutionResult.AssemblyNotFound:
                    attempt.ErrorMessage = "Could not locate assembly";
                    break;

                case ResolutionAttempt.ResolutionResult.IncompatibleVersion:
                    attempt.ErrorMessage = $"Requested version {assemblyName.Version} is incompatible with found version {resultAssemblyName.Version}";
                    break;

                case ResolutionAttempt.ResolutionResult.MismatchedAssemblyName:
                    attempt.ErrorMessage = $"Requested assembly name '{assemblyName.FullName}' does not match found assembly name '{resultAssemblyName.FullName}'";
                    break;
                }
            }

            return(attempt);
        }
            private Assembly ResolveAssembly(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                if (handlerReturn == HandlerReturn.Null)
                {
                    return(null);
                }

                string appPath      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string fileName     = handlerReturn == HandlerReturn.RequestedAssembly ? $"{assemblyName.Name}.dll" : $"{assemblyName.Name}Mismatch.dll";
                string assemblyPath = Path.Combine(appPath, "DependentAssemblies", fileName);

                if (!File.Exists(assemblyPath))
                {
                    return(null);
                }

                Assembly asm  = context.LoadFromAssemblyPath(assemblyPath);
                var      bind = new BindOperation()
                {
                    AssemblyName                  = asm.GetName(),
                    AssemblyPath                  = assemblyPath,
                    AssemblyLoadContext           = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                    RequestingAssembly            = CoreLibName,
                    RequestingAssemblyLoadContext = DefaultALC,
                    Success            = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached             = false
                };

                Binds.Add(bind);
                return(asm);
            }