예제 #1
0
        public static BindOperation AppDomainAssemblyResolveEvent_Exception()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AppDomainAssemblyResolveEvent_Exception));

            using (var handlers = new Handlers(HandlerReturn.Exception))
            {
                Assert.Throws <FileLoadException, BinderTestException>(() => alc.LoadFromAssemblyName(assemblyName));

                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = false,
                    Cached = false,
                    ResolutionAttempts = new List <ResolutionAttempt>()
                    {
                        GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, alc, "Exception in handler for AppDomain.AssemblyResolve")
                    },
                    AppDomainAssemblyResolveHandlers = handlers.Invocations
                });
            }
        }
예제 #2
0
        public static BindOperation ResolveSatelliteAssembly()
        {
            AssemblyName assemblyName = new AssemblyName($"{DependentAssemblyName}.resources");

            assemblyName.CultureInfo = SatelliteCulture;

            CustomALC alc = new CustomALC(nameof(ResolveSatelliteAssembly));

            alc.LoadFromAssemblyPath(Helpers.GetAssemblyInAppPath(DependentAssemblyName));
            Assembly asm = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.ResolveSatelliteAssembly, alc, ResolutionResult.Success, asm)
                }
            });
        }
예제 #3
0
        public static BindOperation ContextualReflection_CustomToDefaultALC()
        {
            CustomALC  alc       = new CustomALC(nameof(ContextualReflection_CustomToDefaultALC));
            Type       testClass = LoadTestClassInALC(alc);
            MethodInfo method    = testClass.GetMethod(nameof(GetDependentAssemblyType), BindingFlags.NonPublic | BindingFlags.Static);

            Type t;

            using (AssemblyLoadContext.Default.EnterContextualReflection())
            {
                t = (Type)method.Invoke(null, new object[0]);
            }

            return(new BindOperation()
            {
                AssemblyName = new AssemblyName(DependentAssemblyName),
                AssemblyLoadContext = DefaultALC,
                RequestingAssembly = testClass.Assembly.GetName(),
                RequestingAssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = t.Assembly.GetName(),
                ResultAssemblyPath = t.Assembly.Location,
                Cached = false,
            });
        }
        public static BindOperation AssemblyInAppPath()
        {
            AssemblyName assemblyName = new AssemblyName(DependentAssemblyName);
            CustomALC    alc          = new CustomALC(nameof(AssemblyInAppPath));
            Assembly     asm          = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ProbedPaths = new List <ProbedPath>()
                {
                    new ProbedPath()
                    {
                        FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.AppPaths, assemblyName.Name, isExe: false),
                        Source = ProbedPath.PathSource.AppPaths,
                        Result = S_OK
                    }
                }
            });
        }
        public static BindOperation AppDomainAssemblyResolve_NameMismatch()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AppDomainAssemblyResolve_NameMismatch));

            using (var handlers = new Handlers(HandlerReturn.NameMismatch))
            {
                // Result of AssemblyResolve event does not get checked for name mismatch
                Assembly asm = alc.LoadFromAssemblyName(assemblyName);

                Assert.AreEqual(1, handlers.Invocations.Count);
                Assert.AreEqual(1, handlers.Binds.Count);
                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached = false,
                    AppDomainAssemblyResolveHandlers = handlers.Invocations,
                    NestedBinds = handlers.Binds
                });
            }
        }
        public static BindOperation SatelliteAssembly_CultureSubdirectory()
        {
            AssemblyName assemblyName = new AssemblyName($"{SubdirectoryAssemblyName}.resources");

            assemblyName.CultureInfo = SatelliteCulture;
            CustomALC alc = new CustomALC(nameof(SatelliteAssembly_CultureSubdirectory));

            alc.LoadFromAssemblyPath(Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName));
            Assembly asm = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ProbedPaths = new List <ProbedPath>()
                {
                    new ProbedPath()
                    {
                        FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, SatelliteCulture.Name, Helpers.GetSubdirectoryPath()),
                        Source = ProbedPath.PathSource.SatelliteSubdirectory,
                        Result = S_OK
                    }
                }
            });
        }
        public static BindOperation AppDomainAssemblyResolve_MultipleHandlers()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AppDomainAssemblyResolve_LoadAssembly));

            using (var handlerNull = new Handlers(HandlerReturn.Null))
                using (var handlerLoad = new Handlers(HandlerReturn.RequestedAssembly))
                {
                    Assembly asm = alc.LoadFromAssemblyName(assemblyName);

                    Assert.AreEqual(1, handlerNull.Invocations.Count);
                    Assert.AreEqual(0, handlerNull.Binds.Count);
                    Assert.AreEqual(1, handlerLoad.Invocations.Count);
                    Assert.AreEqual(1, handlerLoad.Binds.Count);
                    return(new BindOperation()
                    {
                        AssemblyName = assemblyName,
                        AssemblyLoadContext = alc.ToString(),
                        Success = true,
                        ResultAssemblyName = asm.GetName(),
                        ResultAssemblyPath = asm.Location,
                        Cached = false,
                        AppDomainAssemblyResolveHandlers = handlerNull.Invocations.Concat(handlerLoad.Invocations).ToList(),
                        NestedBinds = handlerNull.Binds.Concat(handlerLoad.Binds).ToList()
                    });
                }
        }
예제 #8
0
        public static BindOperation AppDomainAssemblyResolveEvent_CustomALC()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AppDomainAssemblyResolveEvent_CustomALC));

            using (var handlers = new Handlers(HandlerReturn.RequestedAssembly))
            {
                Assembly asm = alc.LoadFromAssemblyName(assemblyName);

                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached = false,
                    ResolutionAttempts = new List <ResolutionAttempt>()
                    {
                        GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, ResolutionResult.AssemblyNotFound),
                        GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, alc, ResolutionResult.Success, asm)
                    },
                    AppDomainAssemblyResolveHandlers = handlers.Invocations
                });
            }
        }
예제 #9
0
        public static BindOperation AssemblyLoadContextLoad()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            var       assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextLoad));

            alc.EnableLoad(assemblyName.Name, assemblyPath);

            Assembly asm = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.Success, asm)
                }
            });
        }
        public static BindOperation SatelliteAssembly_CultureSubdirectory_Lowercase()
        {
            AssemblyName assemblyName = new AssemblyName($"{SubdirectoryAssemblyName}.resources");

            assemblyName.CultureInfo = SatelliteCulture;
            CustomALC alc = new CustomALC(nameof(SatelliteAssembly_CultureSubdirectory));

            alc.LoadFromAssemblyPath(Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName));

            Assembly asm;
            string   subdirectoryPath         = Helpers.GetSubdirectoryPath();
            string   cultureSubdirectory      = Path.Combine(subdirectoryPath, SatelliteCulture.Name);
            string   cultureSubdirectoryLower = Path.Combine(subdirectoryPath, SatelliteCulture.Name.ToLowerInvariant());

            try
            {
                Directory.Move(cultureSubdirectory, cultureSubdirectoryLower);
                asm = alc.LoadFromAssemblyName(assemblyName);
            }
            finally
            {
                Directory.Move(cultureSubdirectoryLower, cultureSubdirectory);
            }

            var probedPaths = new List <ProbedPath>()
            {
                new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, SatelliteCulture.Name, subdirectoryPath),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? COR_E_FILENOTFOUND : S_OK
                }
            };

            // On Linux, the path with a lower-case culture name should also be probed
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                probedPaths.Add(new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, SatelliteCulture.Name.ToLowerInvariant(), subdirectoryPath),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = S_OK
                });
            }

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ProbedPaths = probedPaths
            });
        }
예제 #11
0
        public static BindOperation FindInLoadContext_CustomALC()
        {
            var      assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            Assembly asm          = alcInstance.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alcInstance.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alcInstance, ResolutionResult.Success, asm)
                }
            });
        }
예제 #12
0
        public static BindOperation JITLoad_CustomALC()
        {
            CustomALC  alc       = new CustomALC(nameof(JITLoad_CustomALC));
            Type       testClass = LoadTestClassInALC(alc);
            MethodInfo method    = testClass.GetMethod(nameof(UseDependentAssembly), BindingFlags.NonPublic | BindingFlags.Static);
            Assembly   asm       = (Assembly)method.Invoke(null, new object[0]);

            return(new BindOperation()
            {
                AssemblyName = asm.GetName(),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = testClass.Assembly.GetName(),
                RequestingAssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
예제 #13
0
        public static BindOperation LoadFromAssemblyName()
        {
            AssemblyName assemblyName = new AssemblyName("System.Xml");
            CustomALC    alc          = new CustomALC(nameof(LoadFromAssemblyName));
            Assembly     asm          = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
예제 #14
0
        public static BindOperation LoadFromStream()
        {
            var       executingAssembly = Assembly.GetExecutingAssembly();
            Stream    stream            = File.OpenRead(executingAssembly.Location);
            CustomALC alc = new CustomALC(nameof(LoadFromStream));
            Assembly  asm = alc.LoadFromStream(stream);

            return(new BindOperation()
            {
                AssemblyName = executingAssembly.GetName(),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
예제 #15
0
        public static BindOperation LoadFromAssemblyPath()
        {
            CustomALC alc = new CustomALC(nameof(LoadFromAssemblyPath));
            var       executingAssembly = Assembly.GetExecutingAssembly();
            Assembly  asm = alc.LoadFromAssemblyPath(executingAssembly.Location);

            return(new BindOperation()
            {
                AssemblyName = executingAssembly.GetName(),
                AssemblyPath = executingAssembly.Location,
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = CoreLibName,
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
        public static BindOperation SatelliteAssembly_NotFound()
        {
            string       cultureName  = "en-GB";
            AssemblyName assemblyName = new AssemblyName($"{SubdirectoryAssemblyName}.resources");

            assemblyName.CultureInfo = new CultureInfo(cultureName);
            CustomALC alc = new CustomALC(nameof(SatelliteAssembly_CultureSubdirectory));

            alc.LoadFromAssemblyPath(Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName));
            Assert.Throws <FileNotFoundException>(() => alc.LoadFromAssemblyName(assemblyName));

            var probedPaths = new List <ProbedPath>()
            {
                new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, cultureName, Helpers.GetSubdirectoryPath()),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = COR_E_FILENOTFOUND
                }
            };

            // On Linux (case-sensitive), the path with a lower-case culture name should also be probed
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                probedPaths.Add(new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, cultureName.ToLowerInvariant(), Helpers.GetSubdirectoryPath()),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = COR_E_FILENOTFOUND
                });
            }

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = false,
                Cached = false,
                ProbedPaths = probedPaths
            });
        }
        public static BindOperation ContextualReflection_DefaultToCustomALC()
        {
            Type t;
            CustomALC alc = new CustomALC(nameof(ContextualReflection_DefaultToCustomALC));
            using (alc.EnterContextualReflection())
            {
                t = GetDependentAssemblyType();
            }

            return new BindOperation()
            {
                AssemblyName = new AssemblyName(DependentAssemblyName),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = Assembly.GetExecutingAssembly().GetName(),
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = t.Assembly.GetName(),
                ResultAssemblyPath = t.Assembly.Location,
                Cached = false,
            };
        }
예제 #18
0
        public static BindOperation AssemblyLoadContextLoad_Exception()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            var       assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextLoad), true /*throwOnLoad*/);

            Assert.Throws <FileLoadException, Exception>(() => alc.LoadFromAssemblyName(assemblyName));

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = false,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, $"Exception on Load in '{alc.ToString()}'")
                }
            });
        }
        public static BindOperation AssemblyLoadContextResolving_NameMismatch()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextResolving_NameMismatch));

            using (var handlers = new Handlers(HandlerReturn.NameMismatch, alc))
            {
                Assert.Throws <FileLoadException>(() => alc.LoadFromAssemblyName(assemblyName));

                Assert.AreEqual(1, handlers.Invocations.Count);
                Assert.AreEqual(1, handlers.Binds.Count);
                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = false,
                    Cached = false,
                    AssemblyLoadContextResolvingHandlers = handlers.Invocations,
                    NestedBinds = handlers.Binds
                });
            }
        }
        public static BindOperation AssemblyLoadContextResolving_LoadAssembly()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextResolving_LoadAssembly));

            using (var handlers = new Handlers(HandlerReturn.RequestedAssembly, alc))
            {
                Assembly asm = alc.LoadFromAssemblyName(assemblyName);

                Assert.AreEqual(1, handlers.Invocations.Count);
                Assert.AreEqual(1, handlers.Binds.Count);
                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached = false,
                    AssemblyLoadContextResolvingHandlers = handlers.Invocations,
                    NestedBinds = handlers.Binds
                });
            }
        }
예제 #21
0
        public static BindOperation DefaultAssemblyLoadContextFallback()
        {
            var       assemblyName = new AssemblyName("System.Xml");
            CustomALC alc          = new CustomALC(nameof(DefaultAssemblyLoadContextFallback));
            Assembly  asm          = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.Success, asm),
                    GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.Success, asm)
                }
            });
        }