Startup Class中含有兩個重要方法:Configure方法用於每次http請求的處理,好比後面要講的中間件(Middleware),就是在configure方法中配置。而ConfigureServices方法在Configure方法前調用,它是一個可選的方法,可在configureServices依賴注入接口或一些全局的框架,好比EntityFramework、MVC等。Startup 類的 執行順序:構造 -> configureServices->configure
。html
主要實現一些配置的工做,方法參數以下:json
IHostingEnvironment
: 用於訪問應用程序的特殊屬性,好比applicationName
,applicationVersion
。經過IHostingEnvironment
對象下的屬性能夠在構造中實現配置工做。好比獲取當前根路徑找到配置json文件地址,而後ConfigurationBuilder初始化配置文件,最後能夠經過GetSection()方法獲取配置文件。代碼清單以下:跨域
var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json"); var configuration = builder.Build(); var connStr = configuration.GetSection("Data:DefaultConnection:ConnectionString").Value;
根目錄下的配置文件以下:緩存
{ "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;" } }}
ILoggerFactory
: 提供建立日誌的接口,能夠選用已經實現接口的類或自行實現此接口,下面代碼使用最簡單的控制檯做爲日誌輸出。服務器
public Startup(IHostingEnvironment env, ILoggerFactory logger) { var log = logger.CreateLogger("default"); logger.AddConsole(); log.LogInformation("start configure"); }
主要實現了依賴注入(DI)的配置,方法參數以下:mvc
IServiceCollection:整個ASP.NET Core 默認帶有依賴注入(DI),IServiceCollection是依賴注入的容器,首先建立一個類(Foo)和接口(IFoo),代碼清單以下:app
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace WebApplication1{ public interface IFoo { string GetFoo(); } }
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace WebApplication1{ public class Foo : IFoo { public string GetFoo() { return "foo"; } } }
在ConfigureServices 中將接口和實現注入至容器框架
public void ConfigureServices(IServiceCollection services) { services.AddTransient<ifoo, foo="">(); }
若是想在每次Http請求後都使用IFoo的GetFoo()方法來處理,上面講到能夠在Configure方法中註冊函數,在註冊過程當中因爲使用了依賴注入(DI),所以能夠直接經過RequestServices.GetRequiredService()
泛型方法將IFoo對象在容器中取出。asp.net
app.Run((context) => { var str = context.RequestServices.GetRequiredService().GetFoo(); return context.Response.WriteAsync(str); });
除了本身的接口外,還支持經過擴展方法添加更多的注入方法,好比EntityFramework、mvc框架都實現本身的添加方法。ide
public void ConfigureServices(IServiceCollection services){ // Add framework services. services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<applicationuser, identityrole="">() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddMvc(); // Add application services. services.AddTransient<ifoo, foo="">(); }
主要是http處理管道配置和一些系統配置,參數以下:
IApplicationBuilder
: 用於構建應用請求管道。經過IApplicationBuilder下的run方法傳入管道處理方法。這是最經常使用方法,對於一個真實環境的應用基本上都須要好比權限驗證、跨域、異常處理等。下面代碼調用IApplicationBuilder.Run方法註冊處理函數。攔截每一個http請求,輸出Hello World。
public void Configure(IApplicationBuilder app){ app.Run((context) => context.Response.WriteAsync("Hello World!")); }
IHostingEnvironment
: 同構造參數
ILoggerFactory
: 同構造參數
中間件是一個處理http請求和響應的組件,多箇中間件構成了處理管道(Handler pipeline),每一箇中間件能夠決定是否傳遞至管道中的下一中間件。一旦註冊中間件後,每次請求和響應均會被調用。
中間件的註冊在startup中的Configure方法完成,在configure方法中使用IApplicationBuilder對象的Run、Map、Use方法傳入匿名委託(delegate)。上文示例註冊IFoo.GetFoo()方法就是一個典型的中間件。
Run & Use: 添加一箇中間件至請求管道。它們在功能很相似可是也存在一些區別,先來看下兩個方法的定義。
public static IApplicationBuilder Use(this IApplicationBuilder app, Func<httpcontext, func<task="">, Task> middleware); public static void Run(this IApplicationBuilder app, RequestDelegate handler);
Run是經過擴展方法語法來定義,傳入入參是RequestDelegate的委託,執行完一個第一個run後是不會激活管道中的第二個run方法,這樣代碼執行結果只會輸出一個「hello world!」
app.Run((context) => context.Response.WriteAsync("Hello World!")); app.Run((context) => context.Response.WriteAsync("Hello World 1!"));
而use方法的入參則是Func<>的委託包含兩個入參和一個返回值,這樣在第一個函數執行完成後能夠選擇是否繼續執行後續管道中的中間件仍是中斷。
app.Use((context, next) => { context.Response.WriteAsync("ok"); return next(); }); app.Use((context, next) => { return context.Response.WriteAsync("ok"); });
Map: 含有兩個參數pathMatche和configuration,經過請求的url地址匹配相應的configuration。例如能夠將url路徑是/admin的處理函數指定爲以下代碼:
app.Map("/admin", builder => { builder.Use((context, next) => context.Response.WriteAsync("admin")); });
Middleware | 功能描述 |
---|---|
Authentication | 提供權限支持 |
CORS | 跨域的配置 |
Routing | 配置http請求路由 |
Session | 管理用戶會話 |
Static Files | 提供對靜態文件的瀏覽 |
這裏有一些官方的示例,連接
以上內容有任何錯誤或不許確的地方請你們指正,不喜勿噴!
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。
參考連接
[1] https://docs.asp.net/en/latest/fundamentals/middleware.html
[2] http://www.talkingdotnet.com/app-use-vs-app-run-asp-net-core-middleware/
相關文章: