简体   繁体   English

asp.net 内核中可能有多少“配置”方法的重载?

[英]How many overloads of 'Configure' method are possible in asp.net core?

The Configure method is working like magic in ASP.NET Core 3.1. Configure 方法在 ASP.NET Core 3.1 中像魔术一样工作。

Scenario 1方案 1

When a new project is created, the framework scaffolds the following method signature:当一个新项目被创建时,框架会搭建以下方法签名:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

No surprises that the application works with this signature, because I can presume that the ASP.NET Core framework expects the signature to be as is.应用程序使用此签名并不奇怪,因为我可以假设 ASP.NET 核心框架期望签名保持原样。

Scenario 2方案 2

The second parameter IWebHostEnvironment is removed:第二个参数 IWebHostEnvironment 被移除:

public void Configure(IApplicationBuilder app)

Application works.应用程序工作。

Scenario 3方案 3

Injected my DbContext added to IServiceCollection in 'ConfigureServices' method along with logger:将我的 DbContext 与记录器一起添加到“ConfigureServices”方法中的 IServiceCollection 中:

public void Configure(IApplicationBuilder app, ILogger<Startup> logger, VegaDbContext vegaDbContext)

Surprisingly, application works.令人惊讶的是,应用程序有效。 Looks like the framework is capable enough to resolve types added to service collection.看起来该框架足以解析添加到服务集合的类型。 Good sign.好兆头。

Inline is implementation of ConfigureServices method:内联是 ConfigureServices 方法的实现:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<VegaDbContext>(options =>
            options.UseSqlServer(
                configuration.GetConnectionString("VegaDb")));
    services.AddControllers();
}

Scenario 4情景 4

Injected the WeatherForecastController, which I presume gets added to IServiceCollection via services.AddControllers():注入 WeatherForecastController,我认为它是通过 services.AddControllers() 添加到 IServiceCollection 的:

public void Configure(IApplicationBuilder app, WeatherForecastController weatherForecastController)

Application does not work.应用程序不起作用。 The following exception is thrown:抛出以下异常:

System.Exception: 'Could not resolve a service of type 'Vega.Controllers.WeatherForecastController' for the parameter 'weatherForecastController' of method 'Configure' on type 'Vega.Startup'.' System.Exception:“无法为“Vega.Startup”类型的“Configure”方法的参数“weatherForecastController”解析“Vega.Controllers.WeatherForecastController”类型的服务。

Can some one explain how the method invocation is actually done by the framework and how it is capable of resolving few types like the ILogger and VegaDbContext but not the WeatherForecastController .有人可以解释框架实际上是如何完成方法调用的,以及它如何能够解析ILoggerVegaDbContext等少数类型,但不能解析WeatherForecastController

It works by using the dependency injection infrastructure.它通过使用依赖注入基础设施来工作。 The arguments to Configure are retrieved from the web host's ServiceProvider .从 web 主机的ServiceProvider检索要Configure的 arguments 。 The keyword here is "service"--by default controllers are not added as services to the service collection.这里的关键字是“服务”——默认情况下,控制器不会作为服务添加到服务集合中。

In order to access controllers via dependency injection you need to call the AddControllersAsServices extension method for IMvcCoreBuilder or IMvcBuilder in your ConfigureServices method.为了通过依赖注入访问控制器,您需要在ConfigureServices方法中为IMvcCoreBuilderIMvcBuilder调用AddControllersAsServices扩展方法。

services.AddControllers()
        .AddControllersAsServices();
// or
services.AddControllersWithViews() 
        .AddControllersAsServices();
// or
services.AddMvc()
        .AddControllersAsServices();

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

相关问题 ASP.NET Core 应用程序的 Configure 方法中是否有强制的语句顺序? - Is there a mandatory order of statements in the Configure method of a ASP.NET Core application? 如何在 ASP.NET CORE 3.0 中配置路由以使用带有 [FromQuery] 参数的重载 [HttpGet] 方法? - How to configure routing in ASP.NET CORE 3.0 to use overload [HttpGet] method with [FromQuery] parameters? 如何在asp.net core中startup.cs文件的配置方法中更改范围服务的属性值 - how to change property value of scoped service at configure method of startup.cs file in asp.net core 如何在 ASP.NET Core 中根据请求配置服务 - How to configure services based on request in ASP.NET Core 如何为ASP.net Core配置Entity Framework 6 - How to configure Entity Framework 6 for ASP.net Core 如何使用 ASP.NET Core 7 配置 Brighter? - How do you configure Brighter with ASP.NET Core 7? 如何在ASP.NET Core 1.0中配置身份验证 - How to configure authentication in ASP.NET Core 1.0 如何在 ASP.NET Core 3.1 生产中配置 IdentityServer - How to configure IdentityServer in ASP.NET Core 3.1 production 如何在 Asp.Net Core 应用程序中配置 MassTransit Saga - How to configure MassTransit Saga in Asp.Net Core application 如何在 ASP.NET 内核中自动配置服务? - How to automatically Configure services in ASP.NET core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM