Exemplo n.º 1
0
        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)));
            }
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        public virtual string ResolvePlatformDependencyPathToRuntime(PluginPlatformVersion pluginPlatformVersion, string platformDependencyPath)
        {
            var platformDependencyFileVersion = FileVersionInfo.GetVersionInfo(platformDependencyPath);
            var platformDependencyName        = Path.GetFileName(platformDependencyPath);
            var runtimeInformation            = this.runtimePlatformContext.GetRuntimeInfo();

            var runtimes = runtimeInformation.Runtimes;

            if (pluginPlatformVersion.IsSpecified)
            {
                // First filter on specific version
                runtimes = runtimes.Where(r => r.Version == pluginPlatformVersion.Version);
                // Then, filter on target runtime, this is not always provided
                if (pluginPlatformVersion.Runtime != RuntimeType.UnSpecified)
                {
                    runtimes = runtimes.Where(r => r.RuntimeType == pluginPlatformVersion.Runtime);
                }

                if (!runtimes.Any())
                {
                    throw new AssemblyLoadingException($"Requisted platform was not installed {pluginPlatformVersion.Runtime} {pluginPlatformVersion.Version}");
                }
            }

            foreach (var runtime in runtimes.OrderByDescending(r => r.Version))
            {
                var candidateFilePath = Directory.GetFiles(runtime.Location).FirstOrDefault(f => String.Compare(Path.GetFileName(f), platformDependencyName) == 0);
                if (!String.IsNullOrEmpty(candidateFilePath))
                {
                    var candidateFileVersion = FileVersionInfo.GetVersionInfo(candidateFilePath);
                    if (String.Compare(platformDependencyFileVersion.FileVersion, candidateFileVersion.FileVersion) == 0)
                    {
                        return(candidateFilePath);
                    }
                }
            }

            return(null);
        }