简体   繁体   English

将 .NET 核心从 2 升级到 3 后 Program.cs 中的 DI 停止工作

[英]DI in Program.cs stopped working after upgrade .NET Core from 2 to 3

I have ASP.NET Core application where I need to get database credentials from external AWS service.我有 ASP.NET 核心应用程序,我需要从外部 AWS 服务获取数据库凭证。 Essentially, I need to inject CredentialRetrievalService into Startup.cs.本质上,我需要将 CredentialRetrievalService 注入 Startup.cs。 Awhile ago, I found an example that describes perfectly how to do it:不久前,我找到了一个完美描述如何做到这一点的示例

In Program.cs :Program.cs中:

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureServices(serviceCollection =>
            serviceCollection.AddScoped<ISomeService, SomeService>())
        .UseStartup<Startup>()
        .Build();
}   

and in Startup.cs :Startup.cs中:

private ISomeService _someService;
public Startup(IConfiguration configuration, ISomeService someService)
{
    _someService = someService;
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    // Just to see that everything works - and it does!
    services.AddMvc(options =>
        {
            options.MaxModelValidationErrors = _someService.GetNumber(Configuration.GetValue<int>("MaxModelValidationErrors"));
        });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc();
}

Now I am trying to upgrade the application to ASP.NET Core 3 .现在我正在尝试将应用程序升级到 ASP.NET Core 3 I can see that there is breaking change , and the code above doesn't work any more.我可以看到发生了重大变化,上面的代码不再起作用。 Recommended action is to Inject services into the Startup.Configure method instead.建议的操作是将服务注入Startup.Configure方法。 But what should I do if I need to invoke injected service in ConfigureServices ?但是如果我需要在ConfigureServices中调用注入服务怎么办?

Consider changing the approach and taking advantage of dependency injection when configuring options在配置选项时考虑改变方法并利用依赖注入

Reference Use DI services to configure options参考使用 DI 服务配置选项

public Startup(IConfiguration configuration) {
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services) {

    //...

    services.AddScoped<ISomeService, SomeService>();        

    services.AddMvc(); //options will be configured lower down

    //Use DI services to configure MVC options
    services.AddOptions<MvcOptions>()
        .Configure<ISomeService>((options, someService) => {
            int value = Configuration.GetValue<int>("MaxModelValidationErrors");
            options.MaxModelValidationErrors = someService.GetNumber(value);
        });
        
    //...
}

//...

After more analysis, this is what seems to work:经过更多分析,这似乎是可行的:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddScoped<ISomeService, SomeService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<MvcOptions> options, ISomeService someService)
{
    // ...
    int value = Configuration.GetValue<int>("MaxModelValidationErrors");
    options.Value.MaxModelValidationErrors = someService.GetNumber(value);
    // ...
}

But it doesn't work with my real application where SomeService is DbContext但它不适用于SomeServiceDbContext真实应用程序

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

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