DotNetCore cross-platform ~ Talking about middleware and dotnetcore Middleware
Back to directory
In progress. after the net core platform, it is very easy to add some events in the request process. You can make these events a Middleware, then, these middleware will correspond to each other using the Http pipeline, and they are like a responsibility chain, which is passed down one by one starting from the first middleware you define, until the last middleware is complete!
A few days ago, I wrote about implementing modular services in. net core. DotNetCore is a cross-platform service ~ In the componentization era, we mainly add our defined components to the IServiceCollection set and register them after the program starts. What we want to talk about today is Middleware, which uses IApplicationBuilder, it loads http request-related components after the program starts. These components are processed in the form of pipelines, that is, the middleware we call, the following is the simplest Middleware!
Image search from the Internet
As shown in the figure, a request is recorded, processed by various middleware, and finally responded one by one. Let's take a look at the simple code implementation, which is also an implementation like service componentization, call an extension method and use it in startup.
Middleware in Uncle Lind. DotNetCore framework
ResponseTimeMiddleware implementation
/// <Summary> /// response time middleware /// </summary> public class ResponseTimeMiddleware {private readonly RequestDelegate _ next; public ResponseTimeMiddleware (RequestDelegate next) {_ next = next;} public async Task Invoke (HttpContext context) {Stopwatch sw = new Stopwatch (); sw. start (); Console. writeLine ("ResponseTimeMiddleware... "); await _ next. invoke (context); sw. stop (); Console. writeLine ($ "Page Response Time: {sw. elapsedMilliseconds} ms ");}}
The Extension Method encapsulates it so that it can be used elsewhere.
/// <Summary> /// Lind. dotNetCore. middleware Extension Method // </summary> public static class MiddlewareExtensions {public static IApplicationBuilder UseResponseTime (this IApplicationBuilder builder) {return builder. useMiddleware <ResponseTimeMiddleware> ();} public static IApplicationBuilder UseRequestKey (this IApplicationBuilder builder) {return builder. useMiddleware <RequestKeyMiddleware> ();} public static IApplicationBuilder UseAuthorizationOperation (this IApplicationBuilder builder) {return builder. useMiddleware <AuthorizationOperationMiddleware> ();}}
Use it in startup. Note that it is before the AddMvc method. Otherwise, the api is invalid for your mvc!
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAuthorizationOperation(); app.UseResponseTime(); app.UseRequestKey(); app.UseStaticHttpContext(); app.UseMvc();
In fact, today's middleware is a big highlight in. net core. In fact, I should have written this article for a long time!
Thank you for reading this article!
Back to directory