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
                    }
                }
            });
        }
Пример #2
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
                });
            }
        }
        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
                    }
                }
            });
        }
Пример #4
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)
                }
            });
        }
Пример #5
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
                });
            }
        }
Пример #6
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 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()
                    });
                }
        }
        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_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
            });
        }
Пример #10
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)
                }
            });
        }
Пример #11
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
            });
        }
        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
            });
        }
Пример #13
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
                });
            }
        }
Пример #15
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)
                }
            });
        }
        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
                });
            }
        }