[英]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 .有人可以解释框架实际上是如何完成方法调用的,以及它如何能够解析ILogger和VegaDbContext等少数类型,但不能解析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
方法中为IMvcCoreBuilder
或IMvcBuilder
调用AddControllersAsServices
扩展方法。
services.AddControllers()
.AddControllersAsServices();
// or
services.AddControllersWithViews()
.AddControllersAsServices();
// or
services.AddMvc()
.AddControllersAsServices();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.