Service | Microsoft Docs article | Related commit history on GitHub | Change details |
---|---|---|---|
dev-cross-plat | Resolving Dependency Conflicts | https://github.com/MicrosoftDocs/PowerShell-Docs/commits/staging/reference/docs-conceptual/dev-cross-plat/resolving-dependency-conflicts.md | public class MyModuleInitializer : IModuleAssemblyInitializer // and implements IModuleAssemblyCleanup. public class MyModuleCleanup : IModuleAssemblyCleanup {- public void OnRemove() + public void OnRemove(PSModuleInfo psModuleInfo) { AppDomain.CurrentDomain.AssemblyResolve -= DependencyResolution.ResolveNewtonsoftJson; } namespace AlcModule.Cmdlets protected override Assembly Load(AssemblyName assemblyName) {- // We do the simple logic here of - // looking for an assembly of the given name - // in the configured dependency directory + // We do the simple logic here of looking for an assembly of the given name + // in the configured dependency directory. string assemblyPath = Path.Combine( _dependencyDirPath, $"{assemblyName.Name}.dll"); - // The ALC must use inherited methods to load assemblies - // Assembly.Load*() won't work here - return LoadFromAssemblyPath(assemblyPath); + if (File.Exists(assemblyPath)) + { + // The ALC must use inherited methods to load assemblies. + // Assembly.Load*() won't work here. + return LoadFromAssemblyPath(assemblyPath); + } ++ // For other assemblies, return null to allow other resolutions to continue. + return null; } } } namespace AlcModule.Cmdlets Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Dependencies")); - private static readonly AlcModuleAssemblyLoadContext s_dependencyAlc = new AlcModuleAssemblyLoadContext(s_dependencyDirPath); + private static readonly AlcModuleAssemblyLoadContext s_dependencyAlc = + new AlcModuleAssemblyLoadContext(s_dependencyDirPath); public void OnImport() { namespace AlcModule.Cmdlets AssemblyLoadContext.Default.Resolving += ResolveAlcEngine; } - public void OnRemove() + public void OnRemove(PSModuleInfo psModuleInfo) {- // Remove the Resolving event handler here - AssemblyLoadContext.Default.Resolving -= ResolveAlcEngine; + // Remove the Resolving event handler here + AssemblyLoadContext.Default.Resolving -= ResolveAlcEngine; } - private static Assembly ResolveAlcEngine( - AssemblyLoadContext defaultAlc, - AssemblyName assemblyToResolve) + private static Assembly ResolveAlcEngine(AssemblyLoadContext defaultAlc, AssemblyName assemblyToResolve) { // We only want to resolve the Alc.Engine.dll assembly here. // Because this will be loaded into the custom ALC, |