DotNetCore cross-platform ~ Talking about middleware and dotnetcore Middleware

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.