Middlewares In ASP.NET Core. What is it for? Where to use? 
Tags: middleware, aspnetcore, netcore
Published: 2017-10-02 21:06:39

---
title: Middlewares In ASP.NET Core. What is it for? Where to use?
published: true
description: Today we are going to learn how to injecting little codes in HTTP Pipeline of ASP.NET Core using middlewares. In addition, we are going to learn how construct one, so let us go!
tags: middleware, aspnetcore, .netcore 
---
â€œ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.

<img src="http://stephanybatista.com/wp-content/uploads/2016/05/middlwares.png" />

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.

{% gist 
https://gist.github.com/StephanyBatista/24dcfb7aa1412b3c7c236eb61bc30eb5 %}


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.

{% gist 
https://gist.github.com/StephanyBatista/4bdd053ff1ff4a1a3bf3bf0dd799b806 %}

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.

{% gist 
https://gist.github.com/StephanyBatista/8aa46064e28aa2674255150508852a67 %}


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.

<b>Conclusion</b>

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. 

<b>Did you like this article?</b> 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