Пример #1
0
        protected virtual IServiceProvider GetServiceProvider(IPluginBootstrapper bootstrapper)
        {
            var hostServices   = this.sharedServicesProvider.ProvideHostServices();
            var sharedServices = this.sharedServicesProvider.ProvideSharedServices();
            var allServices    = new ServiceCollection();

            foreach (var service in hostServices)
            {
                allServices.Add(service);
            }

            foreach (var service in sharedServices)
            {
                allServices.Add(service);
            }

            if (bootstrapper != null)
            {
                sharedServices = bootstrapper.Bootstrap(allServices);
            }

            allServices.AddScoped <IPluginServiceProvider>(sp => new DefaultPluginServiceProvider(
                                                               sp,
                                                               hostServices.Select(d => d.ServiceType),
                                                               sharedServices.Select(d => d.ServiceType)
                                                               ));

            return(allServices.BuildServiceProvider());
        }
        public object CreateRemoteInstance(Type pluginType, IPluginBootstrapper bootstrapper, MethodInfo factoryMethod, Assembly assembly)
        {
            var contructors = pluginType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            if (contructors.Count() > 1)
            {
                throw new PrisePluginException($"Multiple public constructors found for remote plugin {pluginType.Name}");
            }

            var firstCtor = contructors.FirstOrDefault();

            if (firstCtor != null && !firstCtor.GetParameters().Any())
            {
                return(assembly.CreateInstance(pluginType.FullName));
            }

            if (factoryMethod == null)
            {
                throw new PrisePluginException($@"Plugins must either provide a default parameterless constructor or implement a static factory method.
                    Like; 'public static {pluginType.Name} CreatePlugin(IServiceProvider serviceProvider)");
            }

            var sharedServices = this.sharedServicesProvider.ProvideSharedServices();

            if (bootstrapper != null)
            {
                sharedServices = bootstrapper.Bootstrap(sharedServices);
            }

            var serviceProvider = sharedServices.BuildServiceProvider();

            return(factoryMethod.Invoke(null, new[] { serviceProvider }));
        }
Пример #3
0
        protected virtual IServiceProvider GetServiceProviderForPlugin(IServiceCollection services, IPluginBootstrapper bootstrapper, IServiceCollection hostServices)
        {
            // Add all the host services to the main collection
            foreach (var service in hostServices)
            {
                services.Add(service);
            }

            IServiceCollection pluginServices = new ServiceCollection();

            // If a bootstrapper was provided, add the services for the plugin to a new collection
            if (bootstrapper != null)
            {
                pluginServices = bootstrapper.Bootstrap(pluginServices);
            }

            // Add all the plugin services to the main collection
            foreach (var service in pluginServices)
            {
                services.Add(service);
            }

            services.AddScoped <IPluginServiceProvider>(sp => this.pluginServiceProviderFactory(
                                                            sp,
                                                            hostServices.Select(d => d.ServiceType),
                                                            pluginServices.Select(d => d.ServiceType)
                                                            ));

            return(services.BuildServiceProvider());
        }
Пример #4
0
        public virtual object CreateRemoteInstance(PluginActivationContext pluginActivationContext, IPluginBootstrapper bootstrapper = null)
        {
            // TODO Check out RuntimeHelpers.GetUninitializedObject(pluginType);
            var pluginType     = pluginActivationContext.PluginType;
            var pluginAssembly = pluginActivationContext.PluginAssembly;
            var factoryMethod  = pluginActivationContext.PluginFactoryMethod;

            var contructors = pluginType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            if (contructors.Count() > 1)
            {
                throw new PrisePluginException($"Multiple public constructors found for remote plugin {pluginType.Name}");
            }

            var firstCtor = contructors.FirstOrDefault();

            if (firstCtor != null && !firstCtor.GetParameters().Any())
            {
                return(pluginAssembly.CreateInstance(pluginType.FullName));
            }

            if (factoryMethod == null)
            {
                throw new PrisePluginException($@"Plugins must either provide a default parameterless constructor or implement a static factory method.
                    Example; 'public static {pluginType.Name} CreatePlugin(IServiceProvider serviceProvider)");
            }

            var hostServices   = this.sharedServicesProvider.ProvideHostServices();
            var sharedServices = this.sharedServicesProvider.ProvideSharedServices();
            var allServices    = new ServiceCollection();

            foreach (var service in hostServices)
            {
                allServices.Add(service);
            }

            foreach (var service in sharedServices)
            {
                allServices.Add(service);
            }

            if (bootstrapper != null)
            {
                sharedServices = bootstrapper.Bootstrap(allServices);
            }

            allServices.AddScoped <IPluginServiceProvider>(sp => new DefaultPluginServiceProvider(
                                                               sp,
                                                               hostServices.Select(d => d.ServiceType),
                                                               sharedServices.Select(d => d.ServiceType)
                                                               ));

            var localProvider = allServices.BuildServiceProvider();

            if (pluginActivationContext.PluginFactoryMethodWithPluginServiceProvider != null)
            {
                var factoryMethodWithPluginServiceProvider = pluginActivationContext.PluginFactoryMethodWithPluginServiceProvider;

                return(factoryMethodWithPluginServiceProvider.Invoke(null, new[] { localProvider.GetService(typeof(IPluginServiceProvider)) }));
            }

            return(factoryMethod.Invoke(null, new[] { localProvider }));
        }