-
Notifications
You must be signed in to change notification settings - Fork 3.1k
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.Net: New Feature: Improve Dependency Injection #8822
Comments
After just digging into implementation current approach might be expensive if someone switch from singleton plugins to scoped/transient. Then every time you call |
@rosieks We do have simplified method for DI, which will register Plugins and Kernel as Transient: semantic-kernel/dotnet/src/SemanticKernel.Abstractions/Services/KernelServiceCollectionExtensions.cs Lines 18 to 34 in 4d73de4
I think if you want to use other lifecycles for your Plugins and Kernel, you can do that by registering your components manually, as shown in your example. Let us know if |
No, it doesn't simplify registration of plugins with respect to lifecycle. |
You can register scoped and singleton plugins and use var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<KernelPlugin>(sp => KernelPluginFactory.CreateFromType<LightsPlugin>(serviceProvider: sp));
serviceCollection.AddSingleton<KernelPlugin>(sp => KernelPluginFactory.CreateFromType<SpeakerPlugin>(serviceProvider: sp));
serviceCollection.AddKernel(); There is also another way with adding plugins using var collection = new ServiceCollection();
var kernelBuilder = collection.AddKernel();
kernelBuilder.Plugins.AddFromType<LightsPlugin>(); // LightsPlugin will be registered as singleton Maybe we can simplify this syntax even more. But if this syntax is not helpful and something is still missing, the title and description of this issue should be updated to emphasize the problem with registering plugins with different lifecycles etc, because currently it mentions the problem in complex syntax in general, while at the moment there are ways how to use easier syntax, which I shared above. We will also need to make sure that simpler syntax is presented in documentation and samples. |
serviceCollection.AddScoped<KernelPlugin>(sp => KernelPluginFactory.CreateFromType<LightsPlugin>(serviceProvider: sp)); The option above is much better than the one presented in documentation, though it still has certain flaws:
I think that having at least such extension method would simplify the whole registration process: public static IKernelBuilder AddPlugin<T>(this IKernelBuilder builder)
{
builder.Services.AddTransient(sp => KernelPluginFactory.CreateFromType<T>(serviceProvider: sp));
return builder;
} Then developer can just simply call: services.AddKernel().AddPlugin<LightsPlugin>() And doesn't care about internal implementation deatils, how Kernel handle plugins. |
Thanks for creating the issue @rosieks we have discussed it and plan to proceed as follows:
|
Please make Dependency Injection easier. According to documentation current way to do that is:
This looks really more like workaround, requires me to understand Kernel internals. What I would prefer is to simplified it to something like that.
This is the same pattern that people are well familiar from ASP.NET Core for registering eg. ActionFilters.
The text was updated successfully, but these errors were encountered: