Dependency Injection KernelBuilder #6471
-
I have a customer who had a question around DI/IoC with regards to KernelBuilder. They have dependencies they inject in the startup of the application for things that managed RAG and other non-kernel related work. ILogger is a good example here. They need to inject that in the startup as well as in the KernelBuilder. Is this the right approach? Does the Kernel run it's own container separate from the rest of the application? If so, is what they are doing the right way (inject in startup and in the KernelBuilder)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
KernelBuilder is typically just for getting started in an environment that lacks its own DI container. The more typical real-app usage is in ASP.NET or Maui or Azure Functions or some other environment where there is a DI container, in which case you just integrate with that (e.g. the |
Beta Was this translation helpful? Give feedback.
KernelBuilder is typically just for getting started in an environment that lacks its own DI container. The more typical real-app usage is in ASP.NET or Maui or Azure Functions or some other environment where there is a DI container, in which case you just integrate with that (e.g. the
AddKernel
extension forIServiceCollection
). See https://youtu.be/jrNfKeGSuCg?t=179, and in particular starting at 15:56 (https://youtu.be/jrNfKeGSuCg?t=957). Kernel itself just wraps an arbitrary IServiceProvider, which can come from anywhere. If you use Kernel.CreateBuilder, that's creating a new standalone service collection that's then passed off to the built Kernel when you call Build. If you instead ju…