public async Task <IAssemblyShim> LoadPluginAssembly(IPluginLoadContext pluginLoadContext) { if (pluginLoadContext == null) { throw new ArgumentNullException(nameof(pluginLoadContext)); } var fullPathToAssembly = pluginLoadContext.FullPathToPluginAssembly.ThrowIfNullOrEmpty(nameof(pluginLoadContext.FullPathToPluginAssembly)); if (!Path.IsPathRooted(fullPathToAssembly)) { throw new AssemblyLoadingException($"FullPathToPluginAssembly {pluginLoadContext.FullPathToPluginAssembly} is not rooted, this must be a absolute path!"); } this.fullPathToPluginAssembly = pluginLoadContext.FullPathToPluginAssembly; this.initialPluginLoadDirectory = Path.GetDirectoryName(fullPathToPluginAssembly); this.nativeDependencyLoadPreference = pluginLoadContext.NativeDependencyLoadPreference; GuardIfAlreadyLoaded(fullPathToPluginAssembly); this.resolver = this.assemblyDependencyResolverFactory(fullPathToPluginAssembly); this.pluginDependencyContext = await this.pluginDependencyContextProvider.FromPluginLoadContext(pluginLoadContext); this.pluginDependencyResolver = this.pluginDependencyResolverFactory(); this.assemblyLoadStrategy = this.assemblyLoadStrategyFactory(); this.pluginPlatformVersion = pluginLoadContext.PluginPlatformVersion ?? PluginPlatformVersion.Empty(); var ensuredPath = this.fileSystemUtilities.EnsureFileExists(fullPathToPluginAssembly); using (var pluginStream = await this.fileSystemUtilities.ReadFileFromDisk(ensuredPath)) { return(new PriseAssembly(LoadAndAddToWeakReferences(pluginStream))); } }
protected virtual void Dispose(bool disposing) { if (!this.disposed && disposing) { this.disposing = true; GC.Collect(); GC.WaitForPendingFinalizers(); if (this.assemblyReferences != null) { foreach (var reference in this.assemblyReferences) { // https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability#use-collectible-assemblyloadcontext for (int i = 0; reference.IsAlive && (i < 10); i++) { GC.Collect(); GC.WaitForPendingFinalizers(); } } } // Unload any loaded native assemblies foreach (var nativeAssembly in this.loadedNativeLibraries) { this.nativeAssemblyUnloader.UnloadNativeAssembly(nativeAssembly.Key, nativeAssembly.Value); } this.loadedPlugins.Clear(); this.assemblyReferences.Clear(); this.loadedNativeLibraries.Clear(); this.pluginDependencyContext?.Dispose(); this.pluginDependencyResolver?.Dispose(); this.resolver?.Dispose(); this.resolver = null; this.loadedNativeLibraries = null; this.loadedPlugins = null; this.assemblyReferences = null; this.assemblyLoadStrategy = null; this.pluginDependencyContext = null; this.pluginDependencyResolver = null; this.fullPathToPluginAssembly = null; this.initialPluginLoadDirectory = null; this.pluginPlatformVersion = null; this.nativeAssemblyUnloader = null; this.pluginDependencyResolverFactory = null; this.assemblyLoadStrategyFactory = null; this.assemblyDependencyResolverFactory = null; this.fileSystemUtilities = null; this.runtimeDefaultAssemblyLoadContext = null; this.pluginDependencyContextProvider = null; } this.disposed = true; }
public LazyLoadResolver(IAssemblyDependencyResolver assemblyDependencyResolver) { _assemblyDependencyResolver = assemblyDependencyResolver; }