The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the other. The following diagram demonstrates the concept. The thread of execution follows the black arrows. Each delegate can perform operations before and after the next delegate
What is ASP.NET Core Request Processing Pipeline?. The Complete ASP.NET Core Developer Course 2023 [Videos].
ASP.NET Core Request Processing Pipeline
In this Video, I am going to discuss the ASP.NET Core Request Processing Pipeline with an example. Please read our previous Video before proceeding to this Video where we discussed Middleware Components in ASP.NET Core application. As part of this Video, we are going to discuss the following pointers in detail.
- Understanding the ASP.NET Core Request Processing Pipeline.
- How to create and register multiple middleware components in ASP.NET Core?
- What is the execution order of middleware components in the request processing pipeline?
Understanding the ASP.NET Core Request Processing Pipeline:
In order to understand the Request Processing Pipeline in ASP.NET Core, concept, let us modify the Configure() method of the Startup class as shown below. Here we are registering three middleware components into the request processing pipeline. As you can see the first two components are registered using the Use() extension method so that they have the chance to call the next middleware component in the request processing pipeline. The last one is registered using the Run() extension method as it is going to be our terminating components i.e. it will not call the next component.
Now run the application and see the browser window as shown in the below image.
Understanding the ASP.NET Core Request Processing Pipeline Execution Order:
In order to understand this, let us compare the above output with the following diagram to understand the ASP.NET Core Request Processing Pipeline in an easier way.
When the incoming HTTP request comes, first it receives by the first middleware component i.e. Middleware1 which logs “Middleware1: Incoming Request” in the response stream. So as a result, first, we see this message first on the browser.
Once the first middleware logs the information, then it calls the next() method which will invoke the second middleware in the request processing pipeline i.e. Middleware2.
The second middleware logs the information “Middleware2: Incoming Request” as a result we see this log information after the first log. Then the second middleware calls the next() which will invoke the third middleware in the request pipeline which is Middleware3.
The third middleware handles the request and then produces the response. So, the third information that we see in the browser is “Middleware3: Incoming Request handled and response generated”.
This middleware component is registered using the Run() extension method, so it is a terminal component. So, from this point, the request pipeline starts reversing. That means from this middleware, the control is given back to the second middleware, and the second middleware logs the information as “Middleware2: Outgoing Response” and then give the control back to the first middleware component, and the first middleware component logs the information as “Middleware1: Outgoing Response” as what we see in the browser.
Key Points to remember:
- The ASP.NET Core request processing pipeline consists of a sequence of middleware components that are going to be called one after the other.
- Each middleware component can perform some operations before and after invoking the next component using the next method. A middleware component can also decide not to call the next middleware component which is called short-circuiting the request pipeline.
- The middleware component in asp.net core has access to both the incoming request and the outgoing response.
- The most important point that you need to keep in mind is the order in which the middleware components are added in the Configure method of the Startup class defines the order in which these middleware components are going to be invoked on requests and the reverse order for the response. So, the order is critical for defining the security, performance, and functionality of the application.