在 .NET Core 中,請求處理管道是一個中間件(Middleware)鏈,用于處理 HTTP 請求并生成響應。管道的底層實現基于 Microsoft.AspNetCore.Http
命名空間中的一些核心類和接口
1. 核心組件
1.1 HttpContext
- ?
HttpContext
是 HTTP 請求和響應的核心抽象,封裝了請求信息(如請求頭、請求體、查詢參數等)和響應信息(如狀態碼、響應頭、響應體等)。 - ? 每個請求都會創建一個
HttpContext
實例,并在整個管道中傳遞。
1.2 HttpRequest
和 HttpResponse
- ?
HttpRequest
和 HttpResponse
分別表示 HTTP 請求和響應,是 HttpContext
的一部分。 - ?
HttpRequest
包含請求的詳細信息,如路徑、方法、頭、體等。 - ?
HttpResponse
用于設置響應的狀態碼、頭、體等。
1.3 RequestDelegate
- ?
RequestDelegate
是一個委托,表示處理 HTTP 請求的方法:
public delegate Task RequestDelegate(HttpContext context);
- ? 管道中的每個中間件都是一個
RequestDelegate
。
1.4 Middleware
- ? 中間件是一個類或方法,用于處理請求并調用管道中的下一個中間件。
- ? 中間件通常通過
Use
或 Run
方法注冊到管道中。
2. 定義 ApplicationBuilder
接口
首先,我們定義 IApplicationBuilder 接口:
public interface IApplicationBuilder
{
IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);
RequestDelegate Build();
IServiceProvider ApplicationServices { get; set; }
IDictionary<string, object> Properties { get; }
}
3. 定義 RequestDelegate 委托
管道的核心是基于委托(Delegate)和上下文(Context)的機制。
**RequestDelegate**
:是一個表示處理 HTTP 請求的委托。
public delegate Task RequestDelegate(HttpContext context);
- ?
**HttpContext**
:封裝了 HTTP 請求和響應的所有信息,包括請求頭、請求體、響應頭、響應體等。
每個中間件本質上是一個 RequestDelegate
,它接收 HttpContext
并處理請求,同時可以選擇調用下一個中間件。
4. 實現 ApplicationBuilder
類
接下來,我們實現 ApplicationBuilder 類:
public class ApplicationBuilder : IApplicationBuilder
{
private readonly IList<Func<RequestDelegate, RequestDelegate>> _components = new List<Func<RequestDelegate, RequestDelegate>>();
public IServiceProvider ApplicationServices { get; set; }
public IDictionary<string, object> Properties { get; } = new Dictionary<string, object>();
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
{
_components.Add(middleware);
return this;
}
public RequestDelegate Build()
{
RequestDelegate app = context =>
{
context.Response.StatusCode = 404;
return Task.CompletedTask;
};
foreach (var component in _components.Reverse())
{
app = component(app);
}
return app;
}
}
5. 創建中間件
我們創建一個簡單的日志收集中間件來展示如何使用 **ApplicationBuilder**
:
public class CustomizeMiddleware
{
private readonly RequestDelegate _next;
public CustomizeMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine("自定義中間件: Before");
await _next(context);
Console.WriteLine("自定義中間件: After");
}
}
6. 使用 ApplicationBuilder
構建管道
public class Program
{
public static void Main(string[] args)
{
var builder = new ApplicationBuilder();
builder.Use(next => new CustomizeMiddleware(next).Invoke);
builder.Use(next => async context =>
{
Console.WriteLine("內聯中間件: Before");
await next(context);
Console.WriteLine("內聯中間件: After");
});
var app = builder.Build();
var context = new DefaultHttpContext();
app(context).Wait();
}
}
輸入結果為:
![](/files/attmgn/2025/1/freeflydom20250116104714306_0.jpg)
7.總結
.NET Core 管道的底層實現是基于委托鏈的機制,每個中間件都是一個 RequestDelegate
,通過鏈式調用來處理 HTTP 請求和響應。管道的構建過程通過 IApplicationBuilder
接口完成,中間件的添加順序決定了管道的執行順序。通過理解管道的底層實現,可以更好地掌握 .NET Core 的請求處理機制,并能夠靈活地配置和擴展管道。
轉自https://www.cnblogs.com/liyongqiang-cc/p/18648546
該文章在 2025/1/16 10:47:32 編輯過