Example #1
0
        protected async Task <T[]> LoadPluginsOfTypeAsync <T>(IPluginLoadOptions <T> pluginLoadOptions)
        {
            var instances  = new List <T>();
            var assemblies = await pluginLoadOptions.AssemblyScanner.Scan();

            if (!assemblies.Any())
            {
                throw new PrisePluginException($"No plugins of type {typeof(T).Name} found after scanning assemblies.");
            }

            assemblies = pluginLoadOptions.AssemblySelector.SelectAssemblies(assemblies);
            if (!assemblies.Any())
            {
                throw new PrisePluginException($@"AssemblySelector returned no assemblies. Requested plugin type: {typeof(T).Name}. Please add the {nameof(PluginAttribute)} to your plugin class and specify the PluginType: [Plugin(PluginType = typeof({typeof(T).Name}))]");
            }

            foreach (var loadContext in assemblies
                     .Select(a => DefaultPluginLoadContext <T> .FromAssemblyScanResult(a))
                     .OrderBy(a => a.PluginAssemblyPath)
                     .ThenBy(a => a.PluginAssemblyName))
            {
                var pluginAssembly = await pluginLoadOptions.AssemblyLoader.LoadAsync(loadContext);

                this.pluginAssemblies.Add(pluginAssembly);
                var pluginInstances = CreatePluginInstances(pluginLoadOptions, ref pluginAssembly);
                instances.AddRange(pluginInstances);
            }

            if (!instances.Any())
            {
                throw new PrisePluginException($"No plugins of type {typeof(T).Name} found from assemblies {String.Join(',', assemblies.Select(a => a.AssemblyPath))}");
            }

            return(instances.OrderBy(t => t.GetType().Name).ToArray());
        }
 public FunctionPluginLoaderOptions(
     IPluginLoadOptions <IHelloPlugin> helloPluginLoadOptions,
     IPluginLogger <IHelloPlugin> pluginLogger,
     IPluginPathProvider <IHelloPlugin> pluginPathProvider,
     IHostTypesProvider <IHelloPlugin> hostTypesProvider,
     IRemoteTypesProvider <IHelloPlugin> remoteTypesProvider,
     IRuntimePlatformContext runtimePlatformContext,
     IHostFrameworkProvider hostFrameworkProvider,
     IDependencyPathProvider <IHelloPlugin> dependencyPathProvider,
     IProbingPathsProvider <IHelloPlugin> probingPathsProvider,
     IPluginDependencyResolver <IHelloPlugin> pluginDependencyResolver,
     INativeAssemblyUnloader nativeAssemblyUnloader,
     ITempPathProvider <IHelloPlugin> tempPathProvider,
     IAssemblyLoadStrategyProvider assemblyLoadStrategyProvider,
     IPluginServerOptions pluginServerOptions,
     IHttpClientFactory httpFactory)
 {
     this.helloPluginLoadOptions       = helloPluginLoadOptions;
     this.pluginLogger                 = pluginLogger;
     this.pluginPathProvider           = pluginPathProvider;
     this.hostTypesProvider            = hostTypesProvider;
     this.remoteTypesProvider          = remoteTypesProvider;
     this.runtimePlatformContext       = runtimePlatformContext;
     this.hostFrameworkProvider        = hostFrameworkProvider;
     this.dependencyPathProvider       = dependencyPathProvider;
     this.probingPathsProvider         = probingPathsProvider;
     this.pluginDependencyResolver     = pluginDependencyResolver;
     this.nativeAssemblyUnloader       = nativeAssemblyUnloader;
     this.tempPathProvider             = tempPathProvider;
     this.assemblyLoadStrategyProvider = assemblyLoadStrategyProvider;
     this.pluginServerOptions          = pluginServerOptions;
     this.httpFactory = httpFactory;
 }
Example #3
0
        protected T[] CreatePluginInstances <T>(IPluginLoadOptions <T> pluginLoadOptions, ref Assembly pluginAssembly)
        {
            var pluginInstances = new List <T>();
            var pluginTypes     = pluginAssembly
                                  .GetTypes()
                                  .Where(t => t.CustomAttributes
                                         .Any(c => c.AttributeType.Name == typeof(Prise.Plugin.PluginAttribute).Name &&
                                              (c.NamedArguments.First(a => a.MemberName == "PluginType").TypedValue.Value as Type).Name == typeof(T).Name))
                                  .OrderBy(t => t.Name)
                                  .AsEnumerable();

            if (pluginTypes == null || !pluginTypes.Any())
            {
                throw new PrisePluginException($@"No plugin was found in assembly {pluginAssembly.FullName}. Requested plugin type: {typeof(T).Name}. Please add the {nameof(PluginAttribute)} to your plugin class and specify the PluginType: [Plugin(PluginType = typeof({typeof(T).Name}))]");
            }

            pluginTypes = pluginLoadOptions.PluginSelector.SelectPlugins(pluginTypes);

            if (!pluginTypes.Any())
            {
                throw new PrisePluginException($@"Selector returned no plugin for {pluginAssembly.FullName}. Requested plugin type: {typeof(T).Name}. Please add the {nameof(PluginAttribute)} to your plugin class and specify the PluginType: [Plugin(PluginType = typeof({typeof(T).Name}))]");
            }

            foreach (var pluginType in pluginTypes)
            {
                var bootstrapperType    = GetPluginBootstrapper(ref pluginAssembly, pluginType);
                var pluginFactoryMethod = GetPluginFactoryMethod(pluginType);

                IPluginBootstrapper bootstrapper = null;
                if (bootstrapperType != null)
                {
                    var remoteBootstrapperInstance = pluginLoadOptions.Activator.CreateRemoteBootstrapper(bootstrapperType, pluginAssembly);
                    var remoteBootstrapperProxy    = pluginLoadOptions.ProxyCreator.CreateBootstrapperProxy(remoteBootstrapperInstance);
                    this.disposables.Add(remoteBootstrapperProxy as IDisposable);
                    bootstrapper = remoteBootstrapperProxy;
                }

                var remoteObject = pluginLoadOptions.Activator.CreateRemoteInstance(pluginType, bootstrapper, pluginFactoryMethod, pluginAssembly);
                var remoteProxy  = pluginLoadOptions.ProxyCreator.CreatePluginProxy(remoteObject, pluginLoadOptions);
                this.disposables.Add(remoteProxy as IDisposable);
                pluginInstances.Add(remoteProxy);
            }

            return(pluginInstances.ToArray());
        }
Example #4
0
 public PrisePluginLoader(IPluginLoadOptions <T> pluginLoadOptions)
 {
     this.pluginLoadOptions = pluginLoadOptions;
 }
Example #5
0
        protected T[] CreatePluginInstances <T>(IPluginLoadOptions <T> pluginLoadOptions, ref Assembly pluginAssembly)
        {
            var pluginInstances = new List <T>();
            var pluginTypes     = pluginLoadOptions.PluginTypesProvider.ProvidePluginTypes(pluginAssembly);

            if (pluginTypes == null || !pluginTypes.Any())
            {
                throw new PrisePluginException($@"No plugin was found in assembly {pluginAssembly.FullName}. Requested plugin type: {typeof(T).Name}. Please add the {nameof(PluginAttribute)} to your plugin class and specify the PluginType: [Plugin(PluginType = typeof({typeof(T).Name}))]");
            }

            foreach (var type in pluginTypes)
            {
                pluginLoadOptions.Logger.PluginTypeProvided(type);
            }

            pluginTypes = pluginLoadOptions.PluginSelector.SelectPlugins(pluginTypes);

            if (!pluginTypes.Any())
            {
                throw new PrisePluginException($@"Selector returned no plugin for {pluginAssembly.FullName}. Requested plugin type: {typeof(T).Name}. Please add the {nameof(PluginAttribute)} to your plugin class and specify the PluginType: [Plugin(PluginType = typeof({typeof(T).Name}))]");
            }

            foreach (var type in pluginTypes)
            {
                pluginLoadOptions.Logger.PluginTypeSelected(type);
            }

            foreach (var pluginType in pluginTypes)
            {
                T pluginProxy = default(T);
                IPluginBootstrapper bootstrapperProxy = null;

                var pluginActivationContext = pluginLoadOptions.PluginActivationContextProvider.ProvideActivationContext(pluginType, pluginAssembly);

                pluginLoadOptions.Logger.PluginActivationContextProvided(pluginActivationContext);

                if (pluginActivationContext.PluginBootstrapperType != null)
                {
                    var remoteBootstrapperInstance = pluginLoadOptions.Activator.CreateRemoteBootstrapper(pluginActivationContext.PluginBootstrapperType, pluginAssembly);
                    pluginLoadOptions.Logger.RemoteBootstrapperActivated(remoteBootstrapperInstance);

                    var remoteBootstrapperProxy = pluginLoadOptions.ProxyCreator.CreateBootstrapperProxy(remoteBootstrapperInstance);
                    pluginLoadOptions.Logger.RemoteBootstrapperProxyCreated(remoteBootstrapperProxy);

                    this.disposables.Add(remoteBootstrapperProxy as IDisposable);
                    bootstrapperProxy = remoteBootstrapperProxy;
                }

                var remoteObject = pluginLoadOptions.Activator.CreateRemoteInstance(
                    pluginActivationContext,
                    bootstrapperProxy
                    );

                pluginLoadOptions.Logger.RemoteInstanceCreated(remoteObject);

                pluginProxy = pluginLoadOptions.ProxyCreator.CreatePluginProxy(remoteObject, pluginLoadOptions.ParameterConverter, pluginLoadOptions.ResultConverter);
                pluginLoadOptions.Logger.RemoteProxyCreated(pluginProxy);

                this.disposables.Add(pluginProxy as IDisposable);
                pluginInstances.Add(pluginProxy);
            }

            return(pluginInstances.ToArray());
        }
Example #6
0
 protected async Task UnloadAsync <T>(IPluginLoadOptions <T> pluginLoadOptions)
 {
     await pluginLoadOptions.AssemblyLoader.UnloadAllAsync();
 }
Example #7
0
 protected void Unload <T>(IPluginLoadOptions <T> pluginLoadOptions)
 {
     pluginLoadOptions.AssemblyLoader.UnloadAll();
 }