Beispiel #1
0
        public async Task <T> LoadPlugin <T>(AssemblyScanResult scanResult, string hostFramework = null, Action <PluginLoadContext> configureLoadContext = null)
        {
            hostFramework = hostFramework.ValueOrDefault(HostFrameworkUtils.GetHostframeworkFromHost());
            var servicesForPlugin = new ServiceCollection();

            var pathToAssembly    = Path.Combine(scanResult.AssemblyPath, scanResult.AssemblyName);
            var pluginLoadContext = PluginLoadContext.DefaultPluginLoadContext(pathToAssembly, typeof(T), hostFramework);

            // This allows the loading of netstandard plugins
            pluginLoadContext.IgnorePlatformInconsistencies = true;

            configureLoadContext?.Invoke(pluginLoadContext);

            var pluginAssembly = await this.assemblyLoader.Load(pluginLoadContext);

            var pluginTypes = this.pluginTypeSelector.SelectPluginTypes <T>(pluginAssembly);
            var pluginType  = pluginTypes.FirstOrDefault(p => p.Name.Equals(scanResult.PluginType.Name));

            if (pluginType == null)
            {
                throw new PluginLoadException($"Did not found any plugins to load from {nameof(AssemblyScanResult)} {scanResult.AssemblyPath} {scanResult.AssemblyName}");
            }

            return(await this.pluginActivator.ActivatePlugin <T>(new DefaultPluginActivationOptions
            {
                PluginType = pluginType,
                PluginAssembly = pluginAssembly,
                ParameterConverter = this.parameterConverter,
                ResultConverter = this.resultConverter,
                HostServices = pluginLoadContext.HostServices
            }));
        }
Beispiel #2
0
        public async Task <IEnumerable <T> > LoadPluginsAsAsyncEnumerable <T>(AssemblyScanResult scanResult, string hostFramework = null, Action <PluginLoadContext> configureLoadContext = null)
        {
#endif
            hostFramework = hostFramework.ValueOrDefault(HostFrameworkUtils.GetHostframeworkFromHost());

            var pathToAssembly    = Path.Combine(scanResult.AssemblyPath, scanResult.AssemblyName);
            var pluginLoadContext = PluginLoadContext.DefaultPluginLoadContext(pathToAssembly, typeof(T), hostFramework);
            // This allows the loading of netstandard plugins
            pluginLoadContext.IgnorePlatformInconsistencies = true;

            configureLoadContext?.Invoke(pluginLoadContext);

            var pluginAssembly = await this.assemblyLoader.Load(pluginLoadContext);

            this.pluginContexts.Add(pluginLoadContext);

            var pluginTypes = this.pluginTypeSelector.SelectPluginTypes <T>(pluginAssembly);

#if SUPPORTS_ASYNC_STREAMS
            foreach (var pluginType in pluginTypes)
            {
                yield return(await this.pluginActivator.ActivatePlugin <T>(new DefaultPluginActivationOptions
                {
                    PluginType = pluginType,
                    PluginAssembly = pluginAssembly,
                    ParameterConverter = this.parameterConverter,
                    ResultConverter = this.resultConverter,
                    HostServices = pluginLoadContext.HostServices
                }));
            }
#else
            var plugins = new List <T>();
            foreach (var pluginType in pluginTypes)
            {
                plugins.Add(await this.pluginActivator.ActivatePlugin <T>(new DefaultPluginActivationOptions
                {
                    PluginType         = pluginType,
                    PluginAssembly     = pluginAssembly,
                    ParameterConverter = this.parameterConverter,
                    ResultConverter    = this.resultConverter,
                    HostServices       = pluginLoadContext.HostServices
                }));
            }
            return(plugins);
#endif
        }