1.媒介

Core与初期版本的 ASP.NET 对照,设置装备摆设运用顺序的体式格局的 Global.asax、FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Startup.cs庖代了。Program.cs作为Web运用顺序的默许进口,在没有任何修正的情况下,会挪用同目录下Startup.cs中的ConfigureServices 和 Configure要领。

2.Startup类

Startup类设置装备摆设效劳和运用的要求管道。Program.Main要领是运用顺序的托管进口。在构建运用顺序的主机(WebHost)时,体系为运用顺序指定 Startup 类,而Main进口经由过程主机天生器(IWebHostBuilder)挪用Build时,天生对应的运用顺序的主机(WebHost),并启动运转(Run)。

public classProgram
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>WebHost.CreateDefaultBuilder(args)
.UseStartup
<Startup>();
}

2.1 当运用顺序启动时挪用 Startup类

当运用顺序启动时,运转时会挪用Startup类的 ConfigureServices 和 Configure要领:

public classStartup
{
//Use this method to add services to the container. public voidConfigureServices(IServiceCollection services)
{
...
}
//Use this method to configure the HTTP request pipeline. public voidConfigure(IApplicationBuilder app)
{
...
}
}

Startup类必需界说Configure要领,然则可选择界说一个ConfigureServices 要领,这些要领将在运用顺序启动时被挪用。下面我们再来了解下这两个要领。

3.ConfigureServices要领

用于设置运用顺序所须要的效劳。
●该要领可选择界说或不界说。
●在Configure要领设置装备摆设运用顺序效劳之前被主机(WebHost)挪用。
●个中按通例设置设置装备摆设选项(appsettings.json)。
关于须要大批设置的功用,IServiceCollection 上有 Add{Service} 扩大要领。 典范 ASP.NET Core 运用将为实体框架(Entity Framework)、标识(Identity)和 MVC 注册效劳:

public voidConfigureServices(IServiceCollection services)
{
//增加 Entity Framework效劳 services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer(
_Configuration.GetConnectionString(
"DefaultConnection")));
services.AddDefaultIdentity
<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores
<ApplicationDbContext>();//增加MVC设置兼容版本效劳. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);//增加运用顺序效劳. services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient
<ISmsSender, AuthMessageSender>();
}

将效劳增加到效劳容器,使其在运用顺序和Configure要领中可用。效劳经由过程依靠干系注入(DI)或 ApplicationServices 举行剖析。

4.Configure要领

用于指定运用顺序相应HTTP要求的体式格局。
可经由过程将中间件(middleware)组件增加到IApplicationBuilder实例来设置装备摆设要求管道。Configure要领可运用 IApplicationBuilder,但未在效劳容器中注册。托管建立 IApplicationBuilder并将其直接通报到Configure。
浅显点来讲,Configure要领用于指定ASP.NET运用顺序将怎样相应每一个HTTP要求,你能够设置装备摆设每一个要求都接收雷同的相应。而更庞杂的管道设置装备摆设能够封装于中间件(middleware)中,并经由过程扩大要领增加到IApplicationBuilder上。Configure要领必需接收一个IApplicationBuilder参数。

4.1 ASP.NET Core模板设置装备摆设的管道支撑:

●开发人员非常页
●非常处置惩罚顺序
●HTTP 严厉传输安全性 (HSTS)
●HTTPS 重定向
●静态文件
●一样平常数据珍爱条例 (GDPR)
●ASP.NET Core MVC 和 Razor Pages

public voidConfigure(IApplicationBuilder app, IHostingEnvironment env)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else{
app.UseExceptionHandler(
"/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();

app.UseMvc();
}

5.总结

●Program的Main要领用于建立WebHost效劳,挪用启动类Startup。
●Startup中的ConfigureServices要领用于将效劳注入到IServiceCollection效劳容器中。
●Startup中的Configure要领用于运用相应HTTP要求,将中间件注册到ApplicationBuilder中来设置装备摆设要求管道。

 

参考文献:

ASP.NET Core 中的运用启动

Last modification:March 25, 2020
如果觉得我的文章对你有用,请随意赞赏