1. 啓動類web
在asp.net5當中,Startup類提供應用程序的入口,對全部應用程序是必須的。爭對特定環境的配置的starup class和方法也是有可能的, 可是,無論怎樣, 一個Startup類做爲整個應用程序的入口。ASP.NET在主要的程序集當中尋找Startup的類名, 你能夠用 Hosting:Application 配置鍵申明一個不一樣的程序集去掉Starup類,無論這個類是定義成公共的仍是其它的, ASP.NET將加載它只要它符號命名規範。若是有多個Startup類,不會引起異常, ASP.NET會選擇一個它命名空間的(符號當前工程根命名空間的優先,不然按字母表的排序的命名空間來用)。app
Starup類可選地在構造函數當中經過DI接受依賴. 一般,應用程序的配置是有Startup構造函數當中定義的。另外Startup類當中也會定義Configure方法,也能夠可選地定義ConfigureServices方法,他們會被調用在應用程序啓動的時候。asp.net
2. 配置方法ide
Configure方法用來指明ASP.NET應用程序怎麼回覆一個單獨的請求。最簡單的,你能夠配置每一個請求返回同一個回覆。然而,真實的應用程序要複雜得多,不少複雜的管道能夠封裝在middleware裏面,能夠用IApplicationBuilder的擴展方法添加。函數
你的Configure方法必須接受一個IApplicationBuilder的參數。另外還有IHostingEnvironment和ILoggerFactory。這些服務會隨着程序的啓動而被注入,下面是一個默認的站點的模板,你能夠看到幾個擴展的方法被用來支持BrowserLink, error pages, static files, ASP.NET MVC和Identity.ui
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)spa
{.net
loggerFactory.AddConsole(Configuration.GetSection("Logging"));日誌
logggerFactory.AddDebug();orm
if(env.IsDevelopment())
{
app.UserBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes => { routes.MapRout( name="default", template: "{controller=Home}/{action=Index}/{id?}"); });
}
the UseMvc擴展方法被定義在BuilderExtensions裏面, 它的首要職責是確保MVC被作爲一個服務添加進來,而且設置了路由。
你能夠用IApplicationBuilder去定義你本身的中間件。
3. ConfigureServices方法
你的Startup類可選地包含一個ConfigureServices方法,用來配置你的應用程序須要的服務。它帶一個IServiceCollection參數,返回一個IServiceProvider, ConfigureServices在Configure以前調用。這很重要,由於一些功能好比MVC,要使它工做正常,須要特定的服務的添加在ConfigureService當中。
和Configure同樣,服務的添加也是由IServiceCollection的擴展函數來添加的。例以下面的例子:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework().AddSqlServer().AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
增長的服務經過DI在應用的時候注入,像Startup同樣,它裏面方法的參數的具體實現也都是由容器注入的,一樣的你的中間件,MVC控制器和其它的類也同樣會注入。
配置類也在ConfigureServices方法當中添加,如:AppSettings.
4. 在Startup裏面已知的服務
你能夠在Startup構造函數或者Configure或者ConfigureServices方法當用應用接口來註冊你本身的服務。下面是默認用到的一些接口:
IApplicationBuilder
這個用來建立應用程序的請求的管道,在Startup類的Configure方法當中用到
IApplicationEnvironment
提供用於訪問應用程序的屬性,如ApplicationName, ApplicationVersion, 和ApplicationBasePath, 在Startup的構造函數和Configure函數裏均可用到。
IHostingEnvironment
提供如當前的EnvironmentName, WebRootPath, 和 web root file provider.
ILoggerFactory
提供建立日誌的機制, 在Startup構造和Configure裏用到。
IServiceCollection
當前窗口配置的服務的集合,在Startup的ConfigureServices方法當中用到。
下面是應用這些接口的順序:
Startup構造函數:IApplicationEnvironment, IHostingEnvironment, ILoggerFactory
ConfigureServices : IServiceCollection
Configure: IApplicationBuilder, IApplicationEnvironment, IHostingEnvironment - ILoggerFactory