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 })); }
protected PluginLoadContext ToPluginLoadContext <T>(AssemblyScanResult plugin) { var hostFramework = HostFrameworkUtils.GetHostframeworkFromHost(); var pathToAssembly = Path.Combine(plugin.AssemblyPath, plugin.AssemblyName); return(PluginLoadContext.DefaultPluginLoadContext(pathToAssembly, typeof(T), hostFramework)); }
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 }
static async Task Main(string[] args) { var mainServiceCollection = new ServiceCollection() .AddSingleton <IConfiguration>(new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddUserSecrets(typeof(Program).Assembly) .Build()) .AddPrise() .AddScoped <IConfigurationService, AppSettingsConfigurationService>(); var serviceProvider = mainServiceCollection.BuildServiceProvider(); var pathToDist = GetPathToDist(); var hostFramework = HostFrameworkUtils.GetHostframeworkFromType(typeof(Program)); var loader = serviceProvider.GetRequiredService <IPluginLoader>(); var configurationService = serviceProvider.GetRequiredService <IConfigurationService>(); var results = await loader.FindPlugins <IPlugin>(pathToDist); foreach (var result in results) { try { var plugin = await loader.LoadPlugin <IPlugin>(result, configure : (context) => { context.IgnorePlatformInconsistencies = true; context.AddHostServices(mainServiceCollection, new[] { typeof(IConfigurationService) }); }); var pluginResults = await plugin.GetAll(); foreach (var pluginResult in pluginResults) { System.Console.WriteLine($"{pluginResult.Text}"); } } catch (PluginActivationException pex) { System.Console.WriteLine($"{pex.Message}"); } catch (ReflectionTypeLoadException tex) { System.Console.WriteLine($"{tex.Message}"); } } }