merken / Prise

A .NET Plugin Framework.
https://merken.github.io/Prise
MIT License
362 stars 40 forks source link

How IConfiguration Got Injected ? #20

Closed Duan112358 closed 4 years ago

Duan112358 commented 4 years ago

First, define Plugin in as follows:

    [Plugin(PluginType = typeof(IPlugin))]
    public class ActivityPlugin : IPlugin
    {
    }

    [PluginBootstrapper(PluginType = typeof(ActivityPlugin))]
    public class PluginBootstrapper : IPluginBootstrapper
    {
        public IServiceCollection Bootstrap(IServiceCollection services)
        {
            var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
            System.Console.WriteLine("elsa config: {0} / {1}", config.GetSection("Elsa").Value, config["Elsa"]);
            var timerConfig = config.GetSection("Elsa:Timers");

            // check value here, if not then throw 
            if (string.IsNullOrEmpty(timerConfig.Value))
            {
                throw new ArgumentNullException("please config Elsa:Timers in appsettings.json");
            }
            return services.AddTimerActivities(options => options.Bind(timerConfig));
        }
    }

Then, host config settings

 // does it is necessary ?
 // services.AddSingleton<IConfiguration>(Configuration);

 // This is required for our razor pages to be found
 // services.AddRazorPages().AddRazorRuntimeCompilation();
  services.AddPrise<IPlugin>(options => {
          var builder = options.WithDefaultOptions(Path.Combine(AppDomain.CurrentDomain.BaseDirectory ?? "", "Plugins"))
       .AddPriseRazorPlugins(Environment.WebRootPath)
       .IgnorePlatformInconsistencies()
       .ScanForAssemblies(composer => composer.UseDiscovery())
        // Find the code in example project, how magic comes in here ?
        .UseHostServices(services, new[] { typeof(IConfiguration) }) 
        .WithAssemblySelector<AssemblyPluginSelector<IPlugin>>();
 });

But, when run the host image image

merken commented 4 years ago

Hi @Duan112358 The IPluginBootstrapper is used to register services that only exists within the space of the plugin, you cannot access host services from here.

Let me run you through what is happening here.

Each plugin receives a completely new ServiceCollection during activation, other than what you might have setup already for your Host application.

You can add Host services to that collection, you can use the UseHostServices method to add these to the Plugin ServiceCollection. image

You may not be able to consume these services, if your Host platform is netcoreapp3.1 and your Plugin is netcoreapp2.1. The IConfiguration from the Host is not exactly the same type as the one expected in the Host. This will never work.

In this scenario, you would create a Bridge from the Plugin back into the Host by using the Prise.PluginBridge package inside your Plugin.

First, add the Prise.PluginBridge reference to your Plugin project: image

https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugin.Microsoft/Translation.Plugin.Microsoft.csproj#L9

Next, in your case, you'll need to do the following;

1. Add a new C# interface file in the Contract called IElsaSettingsProvider that has one method to return the settings image

2. Add a new C# class file in the Host called ConfigElsaSettingsProvider that implements this interface image

3. (Already done by you) Register the IConfiguration as a shared Host service using UseHostServices method in Prise image

4. Register the ConfigElsaSettingsProvider as a SharedService using ConfigureSharedServices method in Prise image

5. Create a new C# class file in the Plugin called ElsaSettingsProviderBridge that will reach out into the Host and call the ConfigElsaSettingsProvider service using the Prise.PluginBridge. image

6. Add a private readonly field called elsaSettingsProvider in your Plugin class with the [PluginService] attribute image

The exact lines, in your case, would be:

[PluginService(ProvidedBy = ProvidedBy.Host, ServiceType = typeof(IElsaSettingsProvider), BridgeType = typeof(ElsaSettingsProviderBridge))]
        private readonly IElsaSettingsProvider elsaSettingsProvider;

You can find a working example of the steps above here: https://github.com/merken/Translation.Plugins

Step 1: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugin.Contract/IPluginConfigurationProvider.cs

Step 2: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugins.Api/PriseCustomizations/AppSettingsPluginConfigurationProvider.cs

Step 3: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugins.Api/Startup.cs#L39

Step 4: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugins.Api/Startup.cs#L43

Step 5: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugin.Microsoft/PluginConfigurationProviderBridge.cs

Step 6: https://github.com/merken/Translation.Plugins/blob/master/Translation.Plugins.Api/Translation.Plugin.Microsoft/MSCogneticServicesTranslationPlugin.cs#L20

If you need more assistance, please share the repo you're working on, I would gladly help setup your project.

merken commented 4 years ago

And by the way: // does it is necessary ? // services.AddSingleton(Configuration);

This is not necessary, because IConfiguration is normally injected by default to your Host...

Duan112358 commented 4 years ago

tks @merken , I will have a try.

You may not be able to consume these services, if your Host platform is netcoreapp3.1 and your Plugin is netcoreapp2.1. The IConfiguration from the Host is not exactly the same type as the one expected in the Host. This will never work.

Plugin and host with the same framework version netcoreapp3.1

btw, why the demo https://github.com/merken/Prise.Examples/tree/master/MVCPlugins works?

Duan112358 commented 4 years ago

I'd desire to register some services when plugin got activated.

    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddTimerActivities(this IServiceCollection services, Action<OptionsBuilder<TimersOptions>> options = null)
        {
            var optionsBuilder = services.AddOptions<TimersOptions>();
            options?.Invoke(optionsBuilder);
            return services
                .AddOptions()
                .AddHostedService<TimersHostedService>()
                .AddActivity<CronEvent>()
                .AddActivity<TimerEvent>()
                .AddActivity<InstantEvent>();
        }
    }
    [PluginBootstrapper(PluginType = typeof(ActivityPlugin))]
    public class PluginBootstrapper : IPluginBootstrapper
    {
        public IServiceCollection Bootstrap(IServiceCollection services)
        {
            var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
            System.Console.WriteLine("elsa config: {0} / {1}", config.GetSection("Elsa").Value, config["Elsa"]);
            var timerConfig = config.GetSection("Elsa:Timers");

            // check value here, if not then throw 
            if (string.IsNullOrEmpty(timerConfig.Value))
            {
                throw new ArgumentNullException("please config Elsa:Timers in appsettings.json");
            }
            return services.AddTimerActivities(options => options.Bind(timerConfig));
        }
    }

The IPluginBootstrapper is used to register services that only exists within the space of the plugin, you cannot access host services from here.

Does Host can consume the services registered in Plugin ?

merken commented 4 years ago

btw, why the demo https://github.com/merken/Prise.Examples/tree/master/MVCPlugins works?

Yes, the example is running on a Linux-based Azure Web App over here: http://prisemvc.mrkn.be

Does Host can consume the services registered in Plugin ?

The Plugin can consume services registered in the Host (via PluginBridge), but not the other way around.

I've not tested Events, yet. So I don't think you can send events from the Plugin to the Host. You can get around this by capturing the event inside the plugin and call upon a shared service from the Host, again, using a PluginBridge.

Duan112358 commented 4 years ago

btw, why the demo https://github.com/merken/Prise.Examples/tree/master/MVCPlugins works?

Yes, the example is running on a Linux-based Azure Web App over here: http://prisemvc.mrkn.be

I wonder why the MVCPlugins Demo can access the Host Configuration without using PluginBridge ?

So, If MVC Demo works, why my Demo doesn't ? what's the differences ?

Or the Azure Web App does the job what the PluginBridge implements do ?

merken commented 4 years ago

I wonder why the MVCPlugins Demo can access the Host Configuration without using PluginBridge ?

You raise a valid point, could you share a repro-project so that I can investigate ?

Duan112358 commented 4 years ago

please check your Github access notifications

merken commented 4 years ago

Please checkout Prise 2.0.0 for more info, this way of injecting services is no longer supported