您的位置:首页 > 编程语言 > ASP

ASP.NET Core 1.0基础之应用启动

2016-01-17 23:04 656 查看
来源https://docs.asp.net/en/latest/fundamentals/startup.html

ASP.NET 5 使得应用对每个http请求有完整的控制权。Startup类是程序的入口,用来设置配置以及wire up一个用程序所使用的服务。开发者也是通过Startup类来配置应用程序的请求管道。

Startup类

Startup是程序入口,是必须的。而且也可以提供基于环境配置的Startup类和方法,详见。ASP.NET通过在主程序集中搜索Startup命名的类来找到入口。你也可以指定Hosting:Application configuration key来使用一个不同的程序集。Startup类是否带public是没有关系的,即使有多个Startup类,也不会触发异常,ASP.NET会基于命名空间来选择一个(先基于项目根命名空间来选择,然后在基于命名空间的字母顺序来选择)。

Startup类可以在构造器中注入依赖,一般情况下应用是通过Startup类的构造器来配置详见。Startup类必须定义个Configure方法,ConfigureServices方法是可选的,他们会在应用启动时调用。

Configure方法

Configure方法指定了应用对每个http请求的响应。最简单的情况,你可以配置每个请求收到同样的响应。但是实际项目是比较负复杂的,因此你可以使用封装好的中间件来通过IApplicationBuilder的扩展方法类配置。

Configure方法必须接收参数IApplicationBuilder,额外的服务如IHostingEnvironment和ILoggerFactory也可以添加到Configure方法的参数中,可以被注入进来。如下默认web模板,通过IApplicationBuilder扩展方法来配置服务BrowserLink, error pages, static files, ASP.NET MVC, and Identity。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

app.UseStaticFiles();

app.UseIdentity();

// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715 
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

你可以通过源码来查看扩展方法。例如UseMvc是定义在BuilderExtensions中的。它的主要责任是确保MVC是被添加((in ConfigureServices)的和配置MVC路由的的。

Middleware 对与中间件可以了解更多以及使用IApplicationBuilder来配置请求管道。

ConfigureServices方法

ConfigureServices方法是可选的,可以配置应用所使用的服务。ConfigureServices 是公共方法,参数是IServiceCollection,可选的返回IServiceProvider。这个ConfigureServices 方法在Configure之前调用。这很重要,因为一些特性如ASP.NET MVC需要先在ConfigureServices中配置,然后才能被加入到http请求管道中。

如Configure方法一样,推荐的做啊是,将需要配置的一些特性添加到IServiceCollection的扩展方法中(it is recommended that features that require substantial setup within ConfigureServices be wrapped up in extension methods on IServiceCollection)。在默认的web模板中,你可以看到有几个Add[Something]扩展方法来配置来自EF和Identiy以及MVC的服务。

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

services.AddMvc();

// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}

通过依赖注入,将服务添加到服务容器中,使得在应用中可用。Startup 类可以指定方法参数的所需依赖,而不是硬编码一个具体实现,你的中间件,mvc controller和其他类也是如此。

ConfigureServices中你也的加入配置选项类,如AppSettings,然后应用中即可用,详见Configuration

Startup类中的可用服务

ASP.NET 5 在Startup类中提供了一些应用服务和对象。可以通过把合适的接口放进Startup的构造器参数里或者它的Configure或ConfigureServices方法的参数里,这些服务即可用的。Startup类中的每个方法的可用服务如下。framework服务和对象包括

IApplicationBuilder 用来构建http请求管道,仅仅在Startup类中的Configure方法中可用,更多详见Request Features

IApplicationEnvironment 提供应用属性,如ApplicationName, ApplicationVersion, and ApplicationBasePath. 在Startup类构造器和Configure方法可用。

IHostingEnvironment Provides the current EnvironmentName, WebRootPath, and web root file provider. Available to the Startup constructor and Configure method.

ILoggerFactory 提供创建日志的机制,在构造器和Congigure方法中可用。详见Logging

IServiceCollection 容器中当前配置的服务集合,只在ConfigureServices中可用,来配置应用中可用的服务。

Startup类中方法调用顺序如下,以及他们可用的服务。

Startup Constructor - IApplicationEnvironment - IHostingEnvironment - ILoggerFactory

ConfigureServices - IServiceCollection

Configure - IApplicationBuilder - IApplicationEnvironment - IHostingEnvironment - ILoggerFactory

尽管ILoggerFactory在构造器中可用,但是一般配置在Configure方法中。详见Logging
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: