简体   繁体   English

从 .Net Core 2.2 迁移到 .Net Core 3.0

[英]Migrate from .Net core 2.2 to .Net Core 3.0

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.嗨,我正在尝试将 Web API 从.Net core 2.2 迁移到 .net core 3.0 我收到以下行代码的警告。 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.警告:从应用程序代码调用 BuildServiceProvider 会导致创建 singleton 服务的额外副本。

Thank you谢谢

It is because you are creating ServiceProvider.这是因为您正在创建 ServiceProvider。 Ideally, you should not call services.BuildServiceProvider().理想情况下,您不应调用 services.BuildServiceProvider()。 it looks like you are calling BuildServiceProvider so that you can resolve IOptions<ConfigurationSettings> .看起来您正在调用BuildServiceProvider以便您可以解析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.而不是在ConfigureServices方法中解析,您可以接受IOptions<ConfigurationSettings>作为服务的参数,ASP.NET Core 会将其注入您的服务。


public class MyService : IMyService
{
    public MyService(IOtherService otherService, IOptions<ConfigurationSettings> configurationSettings)
    {
         // read config value from here.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM