-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Resolve dll loading conflict during help generation #12965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,47 +20,32 @@ | |
| using Tools.Common.Models; | ||
| using Tools.Common.Extensions; | ||
| using System.IO; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace Tools.Common.Loaders | ||
| { | ||
| // TODO: Remove IfDef | ||
| #if NETSTANDARD | ||
| public class CmdletLoader | ||
| #else | ||
| public class CmdletLoader : MarshalByRefObject | ||
| #endif | ||
| { | ||
| public static ModuleMetadata ModuleMetadata; | ||
|
|
||
| public ModuleMetadata GetModuleMetadata(string assemblyPath, List<string> commonOutputFolders) | ||
| { | ||
| AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => | ||
| { | ||
| foreach (var commonOutputFolder in commonOutputFolders) | ||
| { | ||
| var assemblyName = args.Name.Substring(0, args.Name.IndexOf(",")); | ||
| var dll = Directory.GetFiles(commonOutputFolder, "*.dll").FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName); | ||
| if (dll == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| return Assembly.LoadFrom(dll); | ||
| } | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| return GetModuleMetadata(assemblyPath); | ||
| } | ||
| /// <summary> | ||
| /// Use a custom assembly loader context to load assemblies of each module, | ||
| /// so they do not conflict with each other or build tools. | ||
| /// See https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability | ||
| /// and https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7#more-robust-solutions | ||
| /// </summary> | ||
| private CmdletLoaderAlc _alc; | ||
|
|
||
| /// <summary> | ||
| /// Get the ModuleMetadata from a cmdlet assembly. | ||
| /// </summary> | ||
| /// <param name="assmeblyPath">Path to the cmdlet assembly.</param> | ||
| /// <returns>ModuleMetadata containing information about the cmdlets found in the given assembly.</returns> | ||
| public ModuleMetadata GetModuleMetadata(string assemblyPath) | ||
| public ModuleMetadata GetModuleMetadata(string assemblyPath, List<string> commonOutputFolders = null) | ||
| { | ||
| _alc = new CmdletLoaderAlc(commonOutputFolders); | ||
| AssemblyLoadContext.Default.Resolving += CustomResolving; | ||
|
|
||
| var results = new List<CmdletMetadata>(); | ||
|
|
||
| ModuleMetadata = new ModuleMetadata(); | ||
|
|
@@ -208,9 +193,40 @@ public ModuleMetadata GetModuleMetadata(string assemblyPath) | |
| { | ||
| throw ex; | ||
| } | ||
| finally | ||
| { | ||
| AssemblyLoadContext.Default.Resolving -= CustomResolving; | ||
| } | ||
|
|
||
| ModuleMetadata.Cmdlets = results; | ||
| return ModuleMetadata; | ||
| } | ||
|
|
||
| private Assembly CustomResolving(AssemblyLoadContext assemblyLoadContext, AssemblyName name) => _alc.LoadFromAssemblyName(name); | ||
| } | ||
|
|
||
| internal class CmdletLoaderAlc : AssemblyLoadContext | ||
| { | ||
| private readonly List<string> _assemblyDirectories; | ||
|
|
||
| public CmdletLoaderAlc(List<string> assemblyDirectories = null) | ||
| { | ||
| _assemblyDirectories = assemblyDirectories ?? new List<string>(); | ||
| } | ||
|
|
||
| protected override Assembly Load(AssemblyName assemblyName) | ||
| { | ||
| foreach (var assemblyPath in _assemblyDirectories) | ||
| { | ||
| var dll = Directory.GetFiles(assemblyPath, "*.dll").FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name); | ||
| if (dll == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| return LoadFromAssemblyPath(dll); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assembly resolving logic is the same as before. I just use |
||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assembly.LoadFrom()will load the dll into the default ALC, so the dlls may conflict.