Exemplo n.º 1
0
        public virtual object CreateRemoteInstance(IPluginActivationContext pluginActivationContext, IPluginBootstrapper bootstrapper = null, IServiceCollection hostServices = null)
        {
            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 PluginActivationException($"Multiple public constructors found for remote plugin {pluginType.Name}");
            }

            var serviceProvider = AddToDisposables(GetServiceProviderForPlugin(new ServiceCollection(), bootstrapper, hostServices ?? new ServiceCollection())) as IServiceProvider;

            if (factoryMethod != null)
            {
                return(AddToDisposables(factoryMethod.Invoke(null, new[] { serviceProvider })));
            }

            var firstCtor = contructors.FirstOrDefault();

            if (firstCtor != null && !firstCtor.GetParameters().Any()) // Empty default CTOR
            {
                var pluginServiceProvider = AddToDisposables(serviceProvider.GetService <IPluginServiceProvider>()) as IPluginServiceProvider;
                var remoteInstance        = pluginAssembly.Assembly.CreateInstance(pluginType.FullName);
                remoteInstance = InjectPluginFieldsWithServices(remoteInstance, pluginServiceProvider, pluginActivationContext.PluginServices);

                ActivateIfNecessary(remoteInstance, pluginActivationContext);

                return(AddToDisposables(remoteInstance));
            }

            throw new PluginActivationException($"Plugin of type {pluginType.Name} could not be activated.");
        }
Exemplo n.º 2
0
        public virtual object CreateRemoteBootstrapper(IPluginActivationContext pluginActivationContext, IServiceCollection hostServices = null)
        {
            var bootstrapperType     = pluginActivationContext.PluginBootstrapperType;
            var assembly             = pluginActivationContext.PluginAssembly.Assembly;
            var bootstrapperServices = pluginActivationContext.BootstrapperServices;

            var contructors = bootstrapperType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            var firstCtor   = contructors.First();

            if (!contructors.Any())
            {
                throw new PluginActivationException($"No public constructors found for remote bootstrapper {bootstrapperType.Name}");
            }
            if (firstCtor.GetParameters().Any())
            {
                throw new PluginActivationException($"Bootstrapper {bootstrapperType.Name} must contain a public parameterless constructor");
            }
            var bootstrapperInstance        = assembly.CreateInstance(bootstrapperType.FullName);
            var serviceProvider             = AddToDisposables(GetServiceProviderForBootstrapper(new ServiceCollection(), hostServices ?? new ServiceCollection())) as IServiceProvider;
            var bootstrapperServiceProvider = AddToDisposables(serviceProvider.GetService <IBootstrapperServiceProvider>()) as IBootstrapperServiceProvider;

            bootstrapperInstance = InjectBootstrapperFieldsWithServices(bootstrapperInstance, bootstrapperServiceProvider, bootstrapperServices);

            return(AddToDisposables(bootstrapperInstance));
        }
Exemplo n.º 3
0
        protected virtual void ActivateIfNecessary(object remoteInstance, IPluginActivationContext pluginActivationContext)
        {
            var pluginType = pluginActivationContext.PluginType;

            if (pluginActivationContext.PluginActivatedMethod == null)
            {
                return;
            }

            var remoteActivationMethod = remoteInstance.GetType().GetRuntimeMethods().FirstOrDefault(m => m.Name == pluginActivationContext.PluginActivatedMethod.Name);

            if (remoteActivationMethod == null)
            {
                throw new PluginActivationException($"Remote activation method {pluginActivationContext.PluginActivatedMethod.Name} not found for plugin {pluginType.Name} on remote object {remoteInstance.GetType().Name}");
            }

            remoteActivationMethod.Invoke(remoteInstance, null);
        }