Middlewares In ASP.NET Core. What is it for? Where to use?
Stephany Henrique A Oct 2 '17
“Middleware is defined in the OWIN specification as components that form a pipeline between a server and application. It serves to inspect, route, or modify request and response messages for a specific purpose. Middleware consists of application components are incorporated into the ASP.NET HTTP pipeline.”
The text is in the ASP.NET Core documentation, and as can be seen, with middleware we can insert a component that is incorporated in ASP.NET HTTP pipeline, that is, we can develop a code with a purpose to manipulate the requests and responses.
A characteristic of middleware is that normally there will a chain of middleware that are executed by order of configuration. One is called after the end of other and the current middleware can at any moment to cancel, thus all chains of middleware will be canceled.
Do you have doubts about what is middlewares? We can compare middleware with the old http modules that today don’t exist in the new ASP.NET. I have already seen somebody comparing middlewares with a hamburger, because the middle is middleware and the ends are the application and the server. How much more middleware, bigger is the hamburger.
To get more clarity about the subject, I am going to show some middlewares that already are utilized by default in application templates to ASP.NET Core, and after I am going to create a custom middleware.
Below I show the startup class, note that there are some comments that indicate where there is a middleware.
Did you see the middlewares utilized in code? Here, each middleware is a feature to help the project. I don't need to write a code to build a mvc framework, I just insert the instruction "app.UseMvc()".
The gain we have now is that we choose only the ones needed for our project, different from old version of ASP.NET that by default many features were added even when they were not necessary. A very good phrase says: “Pay only by what you use”, in other words, how fewer middleware you use, lighter will be the HTPP pipeline and app performance.
Other thing to observe in code above, all the middlewares start with a “Use”, this is a pattern done by ASP.NET team.
Now I am going to create a custom middleware and my goal will be creating one that shows the total time that the application take for a request. Below following the code.
As can be seen, making a middleware is not hard, it needs a constructor to receive the next middleware and an “Invoke” method that receives the context of HTTP.
The goal of showing the application total time is in the method Invoke. Before and after of calling the next middleware, it is saved the seconds. When all the middlewares already were executed, I execute the code to show in the HTML the total time.
Remember, I know that all middleware were executed because our middleware must be configured first of all others. Thus the code will wait all executions and after the instruction “await _next(context);” we can write our code. Remember, all middlewares works in chain.
The code above is only to show the set up of our middleware in the Configure method in Startup class. In order to works, it is needed using the instruction “app.UseMiddleware();”, where the UseMiddleware method is used to call custom middlewares.
The ASP.NET Core has changed its core and because of this, the way of manipulating a request and a response was changed to middlewares. Now it’s lighter and easier to stay between an application and a server.
Did you like this article? Would you know more about .Net Core? I created a course at Udemy with a cheap price. Access https://www.udemy.com/aspnet-core-20-learn-concepts-and-creating-an-web-app