Giter VIP home page Giter VIP logo

Comments (9)

merken avatar merken commented on June 22, 2024

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.

from prise.

merken avatar merken commented on June 22, 2024

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...

from prise.

Duan112358 avatar Duan112358 commented on June 22, 2024

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?

from prise.

Duan112358 avatar Duan112358 commented on June 22, 2024

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 ?

from prise.

merken avatar merken commented on June 22, 2024

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.

from prise.

Duan112358 avatar Duan112358 commented on June 22, 2024

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 ?

from prise.

merken avatar merken commented on June 22, 2024

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 ?

from prise.

Duan112358 avatar Duan112358 commented on June 22, 2024

please check your Github access notifications

from prise.

merken avatar merken commented on June 22, 2024

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

from prise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.