.NET Core 3.0 中间件 Middleware
2019-11-18杂谈搜奇网41°c
A+ A-中心件官网文档诠释:中心件是一种装配到运用管道以处置惩罚请乞降相应的软件 每一个中心件:
- 挑选是不是将要求通报到管道中的下一个组件。
- 可在管道中的下一个组件前后实行工作。
运用 IApplicationBuilder 建立中心件管道
ASP.NET Core 要求管道包括一系列要求托付,顺次挪用。 下图演示了这一观点。 沿黑色箭头实行。
IApplicationBuilder供应了三个扩大要领设置要求托付
- app.Run 作用增加一个终端中心件,由于不在向下通报要求,经常公开在管道末端运转。实例代码
app.Run(async context => { await context.Response.WriteAsync("Hello, middleware!"); });
- app.Use 将多个要求托付链接在一起。next 参数示意管道中的下一个托付。 可通过不 挪用 next 参数使管道短路等同于aap.run。 一般可在下一个托付前后实行操纵,如以下示例所示:
app.Use(async (context, next) => { // 通报前操纵 await next.Invoke(); // 通报前操纵 }); app.Run(async context => { await context.Response.WriteAsync("Hello from 2nd delegate."); }); }
- Map 扩大用作商定来建立管道分支。 Map 基于给定要求途径的婚配项来建立要求管道分支。 假如要求途径以给定途径开首,则实行分支。实例代码以下
private static void HandleMapTest1(IApplicationBuilder app) { app.Run(async context => { await context.Response.WriteAsync("Map Test 1"); }); } private static void HandleMapTest2(IApplicationBuilder app) { app.Run(async context => { await context.Response.WriteAsync("Map Test 2"); }); } public void Configure(IApplicationBuilder app) { app.Map("/map1", HandleMapTest1); app.Map("/map2", HandleMapTest2); app.Run(async context => { await context.Response.WriteAsync("Hello from non-Map delegate. <p>"); }); }
自定义中心件
以下演示纪录api输入输出参数的中心件。
1.建立一个webapi项目,在默许的WeatherForecastController掌握器中增加一个简朴的post要领,代码以下
[HttpPost] public string PostWeatherForecast([FromBody]WeatherForecastA weatherForecastA) { return "增加胜利"; }
public class WeatherForecastA { public int TemperatureC { get; set; } }
2.新建一个中心件类.CS文件如图
挑选以后默许代码以下:
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project public class LogReqResponseMiddleware { private readonly RequestDelegate _next; public LogReqResponseMiddleware(RequestDelegate next) { _next = next; } public Task Invoke(HttpContext httpContext) { return _next(httpContext); } } // Extension method used to add the middleware to the HTTP request pipeline. public static class LogReqResponseMiddlewareExtensions { public static IApplicationBuilder UseLogReqResponseMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware<LogReqResponseMiddleware>(); } }
脚手架自动帮我们建立一个 Invoke要领,通报给下一个中心件。一个将自定义的中心件增加到了http要求管道的扩大要领UseLogReqResponseMiddleware。
上面invoke不是异步的,我们本身能够修改,以下代码展现 一个api要求的输入参数和输出信息的日记打印
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project public class LogReqResponseMiddleware { private readonly RequestDelegate _next; public LogReqResponseMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext httpContext, ILogger<LogReqResponseMiddleware> logger) { var request = httpContext.Request;
request.EnableBuffering(); //把要求body流转换成字符串 string bodyAsText = await new StreamReader(request.Body).ReadToEndAsync();//纪录要求信息 var requestStr = $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}"; logger.LogDebug("Request:" + requestStr); request.Body.Seek(0, SeekOrigin.Begin); var originalBodyStream = httpContext.Response.Body; using (var responseBody = new MemoryStream()) { httpContext.Response.Body = responseBody; await _next(httpContext); var response = httpContext.Response; response.Body.Seek(0, SeekOrigin.Begin); //转化为字符串 string text = await new StreamReader(response.Body).ReadToEndAsync(); //重新设置偏移量0 response.Body.Seek(0, SeekOrigin.Begin); //纪录返回值 var responsestr = $"{response.StatusCode}: {text}"; logger.LogDebug("Response:" + responsestr); await responseBody.CopyToAsync(originalBodyStream); } } } // Extension method used to add the middleware to the HTTP request pipeline. public static class LogReqResponseMiddlewareExtensions { public static IApplicationBuilder UseLogReqResponseMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware<LogReqResponseMiddleware>(); } }
然后在Startup类的Configure要领中增加下面一行代码,把自定义的中心增加到了HTTP要求的管道中。
app.UseLogReqResponseMiddleware();//纪录http要求 输入、输出值;
我们在postman中模仿要求
掌握台上打印的信息以下:
未定义标签