semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

C#: Plugins: Unable to resolve service for type 'XXXXXX' while attempting to activate 'XXXX_Plugin'

Open nssidhu opened this issue 2 years ago • 3 comments

Describe the bug System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'Plugins.DateTimePlugin'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance[T](IServiceProvider provider, Object[] parameters) at Microsoft.SemanticKernel.KernelPluginFactory.CreateFromType[T](String pluginName, IServiceProvider serviceProvider) at Microsoft.SemanticKernel.KernelExtensions.AddFromType[T](ICollection`1 plugins, String pluginName, IServiceProvider serviceProvider) at AIChatWebApp.Components.Pages.Home.AddDateTimePlugin(Kernel kernel) in

These are the packages that i am using.


 <ProjectReference Include="..\AIChatWebApp.Client\AIChatWebApp.Client.csproj" />
 <PackageReference Include="Azure.Identity" Version="1.10.4" />
 <PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.1.0" />
 <PackageReference Include="Azure.Search.Documents" Version="11.6.0-beta.3" />
 <PackageReference Include="Markdig" Version="0.35.0" />
 <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.3" />
 <PackageReference Include="Microsoft.KernelMemory.Core" Version="0.34.240313.1" />
 <PackageReference Include="Microsoft.KernelMemory.SemanticKernelPlugin" Version="0.34.240313.1" />
 <PackageReference Include="Microsoft.SemanticKernel" Version="1.6.1" />
 <PackageReference Include="Microsoft.SemanticKernel.Connectors.AzureAISearch" Version="1.6.1-alpha" />
 <PackageReference Include="Microsoft.SemanticKernel.Plugins.MsGraph" Version="1.6.1-alpha" />
 <PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.6.1-alpha" />

To Reproduce Create a Plugin with the constructor and try to get a service. Without constructor it works fine.

 public class DateTimePlugin
 {
     private IConfiguration _configuation;
     public DateTimePlugin(IConfiguration configuration)
     {
         _configuation = configuration;
     }

     [KernelFunction]
     [Description("Gets the Date and time for Today.")]
     public string GetDate()
     {
         Console.WriteLine("Invoked GetDate");
         return DateTime.Now.ToString("dd-MMM-yyyy");
     }
 }

Expected behavior I should be able to create a plugin that can use DI to get access to already registered services.

Screenshots If applicable, add screenshots to help explain your problem.

Platform

  • OS: [e.g. Windows, Mac]
  • IDE: [e.g. Visual Studio, VS Code]
  • Language: [e.g. C#, Python]
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository]

Additional context Add any other context about the problem here.

nssidhu avatar Mar 16 '24 21:03 nssidhu

I figured it out, but would also like to confirm if that is the right way to do. Instead of registering service in program.cs, which is mostly done in .net development, I had to register the services on kernel and i can do that only while creating kernel(before kernel is built) and not after the kernel is built

var builder = Kernel.CreateBuilder();
  builder.Services.AddSingleton<IConfiguration>( _configuration); // I have to retrieve it from .net DI container and then add it to kernel. since i am using Blazor i could get them from .net DI container using [Inject]
  ..... Register more service if you have
  ........
 var kernel = builder.Build();

//You cannot add service after the kernel is built.

Now we can get them via DI in the Plugin

 public class DateTimePlugin
 {
     private IConfiguration _configuation;
     public DateTimePlugin(IConfiguration configuration)
     {
         _configuation = configuration;
     }
 .......
 .....

nssidhu avatar Mar 17 '24 23:03 nssidhu

another way to solve this issue is you can pass in a Service provider when you add this plugin: kernel.Plugins.AddFromType("TimePlugin",sp); I don't like this way. it's better to resolve services automatically.

hanhsia avatar Mar 18 '24 00:03 hanhsia

One last option (which we typically recommend when creating a new kernel where you want to reuse services) is to do the following https://github.com/microsoft/semantic-kernel/blob/561a9be716eba6fe0e8793e7d8e1d3b0545bae69/dotnet/samples/HomeAutomation/Program.cs#L85

madsbolaris avatar Mar 19 '24 15:03 madsbolaris