Reputation: 101
Hi I am trying to Migrate Web API from .Net core 2.2 to .net core 3.0 I am getting warning for the below lines code. Could you please let me know how to fix this.
public void ConfigureServices(IServiceCollection services)
{
var buildServiceProvider = services.BuildServiceProvider();
var getService = buildServiceProvider.GetService<IOptions<ConfigurationSettings>>();
ConfigurationSettings = getService.Value;
}
Warning:Calling BuildServiceProvider from application code results in additional copy of singleton services being created.
Thank you
Upvotes: 0
Views: 479
Reputation: 1958
It is because you are creating ServiceProvider. Ideally, you should not call services.BuildServiceProvider(). it looks like you are calling BuildServiceProvider
so that you can resolve IOptions<ConfigurationSettings>
. instead of resolving in the ConfigureServices
method you can accept IOptions<ConfigurationSettings>
as an argument of the service and ASP.NET Core will inject it into your service.
public class MyService : IMyService
{
public MyService(IOtherService otherService, IOptions<ConfigurationSettings> configurationSettings)
{
// read config value from here.
}
}
Upvotes: 1