Closed Duan112358 closed 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.
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:
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
2. Add a new C# class file in the Host called ConfigElsaSettingsProvider that implements this interface
3. (Already done by you) Register the IConfiguration as a shared Host service using UseHostServices method in Prise
4. Register the ConfigElsaSettingsProvider as a SharedService using ConfigureSharedServices method in Prise
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.
6. Add a private readonly field called elsaSettingsProvider in your Plugin class with the [PluginService] attribute
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
If you need more assistance, please share the repo you're working on, I would gladly help setup your project.
And by the way:
// does it is necessary ?
// services.AddSingleton
This is not necessary, because IConfiguration is normally injected by default to your Host...
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?
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 ?
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.
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 ?
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 ?
please check your Github access notifications
Please checkout Prise 2.0.0 for more info, this way of injecting services is no longer supported
First, define Plugin in as follows:
Then, host config settings
But, when run the host