Пример #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)));
            }
        }
Пример #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed && disposing)
            {
                this.disposing = true;

                GC.Collect();
                GC.WaitForPendingFinalizers();

                this.hostTypesProvider        = null;
                this.remoteTypesProvider      = null;
                this.dependencyPathProvider   = null;
                this.probingPathsProvider     = null;
                this.runtimePlatformContext   = null;
                this.depsFileProvider         = null;
                this.pluginDependencyResolver = null;
                this.pluginDependencyContext  = null;
                this.assemblyLoadStrategy     = null;

                // Unload any loaded native assemblies
                foreach (var nativeAssembly in this.loadedNativeLibraries)
                {
                    this.nativeAssemblyUnloader.UnloadNativeAssembly(nativeAssembly.Key, nativeAssembly.Value);
                }

                this.loadedNativeLibraries  = null;
                this.nativeAssemblyUnloader = null;
            }
            this.disposed = true;
        }
Пример #3
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;
        }
Пример #4
0
 protected virtual Assembly LoadAssembly(string assemblyName, IAssemblyLoadStrategy loadStrategy)
 {
     Assembly a = System.Reflection.Assembly.ReflectionOnlyLoad(assemblyName);
     if (a != null) return a;
     System.Reflection.AssemblyName an = new System.Reflection.AssemblyName(assemblyName);
     string asmNameWithExt = an.Name + ".dll";
     foreach (string path in InnerPathes) {
         string assemblyPath = Path.Combine(path, asmNameWithExt);//Path.ChangeExtension(Path.Combine(path, an.Name), "dll");
         if (!File.Exists(assemblyPath)) continue;
         return loadStrategy.Load(assemblyPath);
     }
     return null;
 }
Пример #5
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed && disposing)
            {
                this.disposing = true;

                GC.Collect();
                GC.WaitForPendingFinalizers();

                this.hostTypesProvider        = null;
                this.remoteTypesProvider      = null;
                this.dependencyPathProvider   = null;
                this.probingPathsProvider     = null;
                this.runtimePlatformContext   = null;
                this.depsFileProvider         = null;
                this.pluginDependencyResolver = null;
                this.pluginDependencyContext  = null;
                this.assemblyLoadStrategy     = null;

                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();
                        }
                    }
                }

                this.loadedPlugins.Clear();
                this.loadedPlugins = null;

                this.assemblyReferences.Clear();
                this.assemblyReferences = null;

                // Unload any loaded native assemblies
                foreach (var nativeAssembly in this.loadedNativeLibraries)
                {
                    this.nativeAssemblyUnloader.UnloadNativeAssembly(nativeAssembly.Key, nativeAssembly.Value);
                }

                this.loadedNativeLibraries  = null;
                this.nativeAssemblyUnloader = null;
                this.options = null;
            }
            this.disposed = true;
        }
Пример #6
0
        public virtual Assembly LoadPluginAssembly(IPluginLoadContext pluginLoadContext)
        {
            GuardIfAlreadyLoaded(pluginLoadContext?.PluginAssemblyName);

            this.pluginDependencyContext = PluginDependencyContext.FromPluginAssembly <T>(
                pluginLoadContext,
                this.hostFrameworkProvider,
                this.hostTypesProvider.ProvideHostTypes(),
                this.remoteTypesProvider.ProvideRemoteTypes(),
                this.runtimePlatformContext,
                this.depsFileProvider,
                this.options.IgnorePlatformInconsistencies);

            using (var pluginStream = LoadFileFromLocalDisk(pluginLoadContext.PluginAssemblyPath, pluginLoadContext.PluginAssemblyName))
            {
                this.assemblyLoadStrategy = this.assemblyLoadStrategyProvider.ProvideAssemblyLoadStrategy(this.logger, pluginLoadContext, this.pluginDependencyContext);

                return(base.LoadFromStream(pluginStream)); // ==> AssemblyLoadContext.LoadFromStream(Stream stream);
            }
        }
Пример #7
0
        public virtual async Task <Assembly> LoadPluginAssemblyAsync(IPluginLoadContext pluginLoadContext)
        {
            if (this.pluginDependencyContext != null)
            {
                throw new PrisePluginException($"Plugin {pluginLoadContext.PluginAssemblyName} was already loaded");
            }

            this.pluginDependencyContext = await PluginDependencyContext.FromPluginAssemblyAsync(
                pluginLoadContext,
                this.hostFrameworkProvider,
                this.hostTypesProvider.ProvideHostTypes(),
                this.remoteTypesProvider.ProvideRemoteTypes(),
                this.runtimePlatformContext,
                this.depsFileProvider,
                this.options.IgnorePlatformInconsistencies);

            using (var pluginStream = await LoadFileFromLocalDiskAsync(pluginLoadContext.PluginAssemblyPath, pluginLoadContext.PluginAssemblyName))
            {
                this.assemblyLoadStrategy = this.assemblyLoadStrategyProvider.ProvideAssemblyLoadStrategy(pluginLoadContext, this.pluginDependencyContext);

                return(base.LoadFromStream(pluginStream)); // ==> AssemblyLoadContext.LoadFromStream(Stream stream);
            }
        }
Пример #8
0
        public virtual async Task <Assembly> LoadPluginAssemblyAsync(IPluginLoadContext pluginLoadContext)
        {
            GuardIfAlreadyLoaded(pluginLoadContext?.PluginAssemblyName);

            this.pluginDependencyContext = await PluginDependencyContext.FromPluginAssemblyAsync(
                pluginLoadContext,
                this.logger,
                this.hostFrameworkProvider,
                this.hostTypesProvider.ProvideHostTypes(),
                this.hostTypesProvider.ProvideHostAssemblies(),
                this.downgradableDependenciesProvider.ProvideDowngradableTypes(),
                this.downgradableDependenciesProvider.ProvideDowngradableAssemblies(),
                this.remoteTypesProvider.ProvideRemoteTypes(),
                this.runtimePlatformContext,
                this.depsFileProvider,
                this.options.IgnorePlatformInconsistencies);

            using (var pluginStream = await LoadFileFromLocalDiskAsync(pluginLoadContext.PluginAssemblyPath, pluginLoadContext.PluginAssemblyName))
            {
                this.assemblyLoadStrategy = this.assemblyLoadStrategyProvider.ProvideAssemblyLoadStrategy(this.logger, pluginLoadContext, this.pluginDependencyContext);
                return(LoadAndAddToWeakReferences(pluginStream));
            }
        }