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 static PluginLoadContext AddDowngradableHostAssemblies(this PluginLoadContext pluginLoadContext, IEnumerable <string> assemblies)
 {
     if (assemblies == null || !assemblies.Any())
     {
         return(pluginLoadContext); // short circuit
     }
     pluginLoadContext.DowngradableHostAssemblies = new List <string>(pluginLoadContext.DowngradableHostAssemblies.Union(assemblies));
     return(pluginLoadContext);
 }
Beispiel #3
0
 public static PluginLoadContext AddDowngradableHostTypes(this PluginLoadContext pluginLoadContext, IEnumerable <Type> downgradableHostTypes)
 {
     if (downgradableHostTypes == null || !downgradableHostTypes.Any())
     {
         return(pluginLoadContext); // short circuit
     }
     pluginLoadContext.DowngradableHostTypes = new List <Type>(pluginLoadContext.DowngradableHostTypes.Union(downgradableHostTypes));
     return(pluginLoadContext);
 }
Beispiel #4
0
 public static PluginLoadContext AddRemoteTypes(this PluginLoadContext pluginLoadContext, IEnumerable <Type> remoteTypes)
 {
     if (remoteTypes == null || !remoteTypes.Any())
     {
         return(pluginLoadContext); // short circuit
     }
     pluginLoadContext.RemoteTypes = new List <Type>(pluginLoadContext.RemoteTypes.Union(remoteTypes));
     return(pluginLoadContext);
 }
Beispiel #5
0
 public static PluginLoadContext AddAdditionalProbingPaths(this PluginLoadContext pluginLoadContext, IEnumerable <string> additionalProbingPaths)
 {
     if (additionalProbingPaths == null || !additionalProbingPaths.Any())
     {
         return(pluginLoadContext); // short circuit
     }
     pluginLoadContext.AdditionalProbingPaths = new List <string>(pluginLoadContext.AdditionalProbingPaths.Union(additionalProbingPaths));
     return(pluginLoadContext);
 }
Beispiel #6
0
        public static PluginLoadContext AddHostService(this PluginLoadContext pluginLoadContext, ServiceDescriptor hostService)
        {
            // Add the Host service to the servicecollection of the plugin
            pluginLoadContext.HostServices.Add(hostService);

            return(pluginLoadContext
                   // A host type will always live inside the host
                   .AddHostTypes(new[] { hostService.ServiceType })
                   // The implementation type will always exist on the Host, since it will be created here
                   .AddHostTypes(new[] { hostService.ImplementationType ?? hostService.ImplementationInstance?.GetType() ?? hostService.ImplementationFactory?.Method.ReturnType }));
        }
Beispiel #7
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
        }
Beispiel #8
0
        public static PluginLoadContext AddHostServices(this PluginLoadContext pluginLoadContext,
                                                        IServiceCollection hostServices,
                                                        IEnumerable <Type> includeTypes = null,
                                                        IEnumerable <Type> excludeTypes = null)
        {
            if (includeTypes == null || !includeTypes.Any())
            {
                return(pluginLoadContext); // short circuit
            }
            var hostTypes       = new List <Type>();
            var priseServices   = hostServices.Where(s => IsPriseService(s.ServiceType));
            var includeServices = hostServices.Where(s => Includes(s.ServiceType, includeTypes));
            var excludeServices = hostServices.Where(s => Excludes(s.ServiceType, excludeTypes));

            foreach (var hostService in hostServices
                     .Except(priseServices)
                     .Union(includeServices)
                     .Except(excludeServices))
            {
                pluginLoadContext.AddHostService(hostService);
            }

            return(pluginLoadContext);
        }
Beispiel #9
0
 public static PluginLoadContext AddHostService <T>(this PluginLoadContext pluginLoadContext, T implementation, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
 {
     return(pluginLoadContext.AddHostService(new ServiceDescriptor(typeof(T), (s) => implementation, serviceLifetime)));
 }
Beispiel #10
0
 public static PluginLoadContext AddHostService(this PluginLoadContext pluginLoadContext, Type hostServiceType, Type hostServiceImplementationType, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
 {
     return(pluginLoadContext.AddHostService(new ServiceDescriptor(hostServiceType, hostServiceImplementationType, serviceLifetime)));
 }
Beispiel #11
0
 public static PluginLoadContext SetRuntimePlatformContext(this PluginLoadContext pluginLoadContext, IRuntimePlatformContext runtimePlatformContext)
 {
     pluginLoadContext.RuntimePlatformContext = runtimePlatformContext;
     return(pluginLoadContext);
 }
Beispiel #12
0
 public static PluginLoadContext SetPlatformVersion(this PluginLoadContext pluginLoadContext, PluginPlatformVersion pluginPlatformVersion)
 {
     pluginLoadContext.PluginPlatformVersion = pluginPlatformVersion;
     return(pluginLoadContext);
 }
Beispiel #13
0
 public static PluginLoadContext SetNativeDependencyLoadPreference(this PluginLoadContext pluginLoadContext, NativeDependencyLoadPreference nativeDependencyLoadPreference)
 {
     pluginLoadContext.NativeDependencyLoadPreference = nativeDependencyLoadPreference;
     return(pluginLoadContext);
 }