What is Middleware ?

Şeyma Arslan
4 min readDec 11, 2021

--

One of the most important features of ASP.NET Core is the “Middleware” structure. It is a very effective feature for performing customized operations in the middle of the “Request-Response” model and managing the request-response traffic. We can do many different needs such as checking the validity of an incoming request, creating the responses from the cache, in ASP.NET Core with the structures we put together.

A structure that enables the execution of methods added as an add-on to a class derived from the IApplicationBuilder interface on the basis of middlewares.

We use it when we want to perform different operations and give a different direction to the course of the process, until the response to the request coming from the client to the web application.

How Middleware Works?

Middleware is a terminological structure that exists in the programming world independently of Asp.Net. It is a structure that has the same operating mechanism everywhere. Middlewares are triggered windingly. When the middleware is triggered, it triggers another Middleware before it expires itself.

In the image above, a request is received and the 1st Middleware is triggered and in this place, the requested operations are performed, then the next command triggers the next Middleware. It continues to be triggered in this way until the third Middleware, then because there is no Middleware to trigger, it returns to the previous Middleware, completes it and returns to the previous one, and when the last one is completed, the response returns. In this way, the spiral is completed.

Asp.Net Core has a core that structurally supports Middleware structuring. All functions in the Configure Method in the startup file actually act as Middleware. In Asp.Net Core structuring, middlewares start with the name “Use” and are called in Configure.

The trigger order is important in Middlewares. Therefore, Middlewares should be sorted by paying attention to the working priority. For example, if authentication and authorization are to be done, the authentication Middleware should be called first and then the authorization Middleware should be called. Ignoring the order here will cause logical errors.

In Asp.Net Core, there are middlewares that is set in the core. These; Run, Use, Map, MapWhen.

Run: It does not trigger the Middleware that comes after itself. As a result, the pupline does not continue and will give direct output. This effect is called short circuit. It can be used in accordance with the operation you will perform.

Use: The Use Method calls the next middleware in the process after it is activated and has a structure that can go back and continue after the normal Middleware function is finished.

Map: Sometimes we may want to filter the Middleware according to the path which sends request. For this, we can provide if control in Use or Run functions, or we can do more professional operations with the Map method.

MapWhen: With the Map Method, while filtering is performed only according to the path where the request is made, filtering is performed according to any feature of the incoming request with the MapWhen Method.

Now let’s unify and create a general error trapping mechanism using the Middleware that comes with .NET Core and log the errors we receive. I created my project as WepApi. I created a new class and named it LoggingMiddleware. It consists of a constructor and the Invoke Method that takes a simple RequestDelegate parameter. We will catch the exception by taking the request to the try catch blog and write the error we caught in a txt file.

Let’s add the following code to the Startup.cs class for our application to be included in the pipeline.

Let’s make a request in Postman and then check our log.txt file.

In the image below, we can see that the log file is created and running without any errors.Thanks for reading.

--

--