In this article, I am going to discuss Configuring Middleware Components in the ASP.NET Core application to handle the request processing pipeline. Please read our previous article before proceeding to this article where we discussed the basics of ASP.NET Core Middleware Components. As part of this article, we are going to discuss the following pointers.
As we already discussed in our previous article that we need to configure the middleware component within the Configure method of the Startup class which is present within the Startup.cs file. So when we create an ASP.NET Core application with the Empty template, then by default the following Startup class is created with the Configure() method as shown in the below image.
What are Request Delegates in ASP.NET Core?
In ASP.NET Core, Request delegates are used to build the request pipeline i.e. request delegates are used to handle each incoming HTTP request. In ASP.NET Core, you can configure the Request delegates using the Run, Map, and Use extension methods. You can specify a request delegate using an in-line anonymous method (called in-line middleware) or you can specify the request delegates using a reusable class. These reusable classes and in-line anonymous methods are called as middleware or middleware components. Each middleware component in the request processing pipeline is responsible for invoking the next component in the pipeline or short-circuiting the pipeline by not calling the next middleware component.
What is the use of Use and Run methods in ASP.NET Core Web Application?
In ASP.NET Core, you can use the “Use” and “Run” extension methods to register the Inline Middleware component into the Request processing pipeline. The “Run” extension method allows us to add the terminating middleware (the middleware which will not call the next middleware components in the request processing pipeline). On the other hand, the “Use” extension method allows us to add the middleware components which may call the next middleware component in the request processing pipeline.
If you observe the Configure method, then you will see that it gets an instance of the IApplicationBuilder interface, and using that instance along with the extension methods such as Use and Run, it configures the Middleware components.
As you can see, in the configure method, two middleware components are registered in the request processing pipeline using the IApplicationBuilder instance i.e. app. They are as follows
What is the use of DeveloperExceptionPage Middleware Component?
As you can see, within the configure method, the UseDeveloperExceptionPage() middleware component is registered into the pipeline and this middleware will come into the picture only when the hosting environment is “development”. This middleware component is going to execute when there is an unhandled exception occurred in the application and since it is in development mode, it is going to show you the culprit line of code. You can consider this as a replacement for the yellow screen of death. In a later article, we will see the use of this middleware component with examples.
How to Configure Middleware Components using the Run() extension method?
The second middleware component is registered by using the Run() extension method. As this component is registered using the Run extension method, so it is going to be a terminating middleware component means it will not call the next middleware component in the request processing pipeline. This middleware component is simply going to write the “Hello World!” message on to the Response object. The points that you need to remember is, this is the component which will respond to each and every incoming HTTP request at the moment.
The second middleware component is registered by using the Run() extension method. As this component is registered using the Run extension method, so it is going to be a terminating middleware component means it will not call the next middleware component in the request processing pipeline. It simply going to write the “Hello World!” message on to the Response object.
At the moment if you run the application, then you will see the “Hello World!” message irrespective of the URL Path. That means all the incoming requests are handled by this middleware (Run) component only.
Run() method in details:
We are invoking the Run() method on the IApplicationBuilder instance (app) to register the middleware component into the request processing pipeline. Following is the definition of the Run method.
As you can see from the definition of the Run() method, it is implemented as an extension method of the IApplicationBuilder interface. This is the reason why we are able to call the Run() method using the IApplicationBuilder instance i.e. app. If you are new to the extension method then please read the below article where we discussed the extension methods in detail.
https://csharptechtics.blogspot.com/2020/08/extension-methods-csharp.html
You can also see from the above image that the Run() method takes an input parameter of type RequestDelegate. Following is the definition of RequestDelegate.
As you can see from the above image, the RequestDelegate is a delegate that takes an input parameter of type HttpContext object. If you are new to delegates then I strongly recommended you read the following article where we discussed the delegates in detail.
https://csharptechtics.blogspot.com/2020/08/delegates-csharp.html
As we already discussed the middleware components in ASP.NET Core application can have access to both HTTP Request and Response and this is because of the above HttpContext object.
In our example, we are passing the request delegate inline as an anonymous method using lambda expression and moreover we passing the HTTPContext object as an input parameter to the request delegate. The following diagram shows the above
Note: Instead of passing the request delegate inline as an anonymous method, you can also define the request delegate in a separate class and pass it here which we will discuss in a later article.
Example:
Let us create another middleware using the Run() extension method. To do this, modify the Configure method of the Startup class as shown below.
Getting Response from 1st Middleware
The output is coming from the first middleware component. The reason is, when we registered a middleware component using the Run() extension method then that component becomes a terminal component means it will not call the next middleware component in the request processing pipeline.
Then the question that comes to your mind is how to call the next component in the request processing pipeline, the answer is registered your middleware component using the Use extension method as shown below.
Understanding the Use extension method:
The Use extension method adds a middleware delegate defined in-line to the application’s request pipeline. Following is the definition of the Use extension method:
This method is also implemented as an extension method on the IApplicationBuilder interface. This is the reason why we are able to invoke this method using the IApplicationBuilder instance. As you can see from the above definition, this method takes two input parameters. The first parameter is the HttpContext context object through which it can access both the HTTP request and response. The second parameter is the Func type i.e. it is a generic delegate that can handle the request or call the next middleware component in the request pipeline.
That’s it for today. In the next article, I am going to discuss the ASP.NET Core request processing pipeline with an example. Here, in this article, I try to explain how to Configuring Middleware Components in the ASP.NET Core application to handle the request processing pipeline with an example.
Summary:
I hope this post will be helpful to understand the concept of Configuring Middleware Components in ASP.NET Core
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
- How to Configure Middleware Components in ASP.NET Core application?
- What is Request Delegates in ASP.NET Core?
- What are Use and Run methods in ASP.NET Core?
- What us UseDeveloperExceptionPage Middleware Component?
- How to Configure Middleware Components using the Run() extension method?
- What is Use extension method?
As we already discussed in our previous article that we need to configure the middleware component within the Configure method of the Startup class which is present within the Startup.cs file. So when we create an ASP.NET Core application with the Empty template, then by default the following Startup class is created with the Configure() method as shown in the below image.
What are Request Delegates in ASP.NET Core?
In ASP.NET Core, Request delegates are used to build the request pipeline i.e. request delegates are used to handle each incoming HTTP request. In ASP.NET Core, you can configure the Request delegates using the Run, Map, and Use extension methods. You can specify a request delegate using an in-line anonymous method (called in-line middleware) or you can specify the request delegates using a reusable class. These reusable classes and in-line anonymous methods are called as middleware or middleware components. Each middleware component in the request processing pipeline is responsible for invoking the next component in the pipeline or short-circuiting the pipeline by not calling the next middleware component.
What is the use of Use and Run methods in ASP.NET Core Web Application?
In ASP.NET Core, you can use the “Use” and “Run” extension methods to register the Inline Middleware component into the Request processing pipeline. The “Run” extension method allows us to add the terminating middleware (the middleware which will not call the next middleware components in the request processing pipeline). On the other hand, the “Use” extension method allows us to add the middleware components which may call the next middleware component in the request processing pipeline.
If you observe the Configure method, then you will see that it gets an instance of the IApplicationBuilder interface, and using that instance along with the extension methods such as Use and Run, it configures the Middleware components.
As you can see, in the configure method, two middleware components are registered in the request processing pipeline using the IApplicationBuilder instance i.e. app. They are as follows
- UseDeveloperExceptionPage() Middleware Component
- Middleware Component Registered using Run() method
What is the use of DeveloperExceptionPage Middleware Component?
As you can see, within the configure method, the UseDeveloperExceptionPage() middleware component is registered into the pipeline and this middleware will come into the picture only when the hosting environment is “development”. This middleware component is going to execute when there is an unhandled exception occurred in the application and since it is in development mode, it is going to show you the culprit line of code. You can consider this as a replacement for the yellow screen of death. In a later article, we will see the use of this middleware component with examples.
How to Configure Middleware Components using the Run() extension method?
The second middleware component is registered by using the Run() extension method. As this component is registered using the Run extension method, so it is going to be a terminating middleware component means it will not call the next middleware component in the request processing pipeline. This middleware component is simply going to write the “Hello World!” message on to the Response object. The points that you need to remember is, this is the component which will respond to each and every incoming HTTP request at the moment.
The second middleware component is registered by using the Run() extension method. As this component is registered using the Run extension method, so it is going to be a terminating middleware component means it will not call the next middleware component in the request processing pipeline. It simply going to write the “Hello World!” message on to the Response object.
At the moment if you run the application, then you will see the “Hello World!” message irrespective of the URL Path. That means all the incoming requests are handled by this middleware (Run) component only.
Run() method in details:
We are invoking the Run() method on the IApplicationBuilder instance (app) to register the middleware component into the request processing pipeline. Following is the definition of the Run method.
As you can see from the definition of the Run() method, it is implemented as an extension method of the IApplicationBuilder interface. This is the reason why we are able to call the Run() method using the IApplicationBuilder instance i.e. app. If you are new to the extension method then please read the below article where we discussed the extension methods in detail.
https://csharptechtics.blogspot.com/2020/08/extension-methods-csharp.html
You can also see from the above image that the Run() method takes an input parameter of type RequestDelegate. Following is the definition of RequestDelegate.
As you can see from the above image, the RequestDelegate is a delegate that takes an input parameter of type HttpContext object. If you are new to delegates then I strongly recommended you read the following article where we discussed the delegates in detail.
https://csharptechtics.blogspot.com/2020/08/delegates-csharp.html
As we already discussed the middleware components in ASP.NET Core application can have access to both HTTP Request and Response and this is because of the above HttpContext object.
In our example, we are passing the request delegate inline as an anonymous method using lambda expression and moreover we passing the HTTPContext object as an input parameter to the request delegate. The following diagram shows the above
Note: Instead of passing the request delegate inline as an anonymous method, you can also define the request delegate in a separate class and pass it here which we will discuss in a later article.
Example:
Let us create another middleware using the Run() extension method. To do this, modify the Configure method of the Startup class as shown below.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Run(async (context) => { await context.Response.WriteAsync("Getting Response from 1st Middleware"); }); app.Run(async (context) => { await context.Response.WriteAsync("Getting Response from 2nd Middleware"); }); }Now we have two middleware components registered with the Run() extension method. If you run the application then you will get the following output.
Getting Response from 1st Middleware
The output is coming from the first middleware component. The reason is, when we registered a middleware component using the Run() extension method then that component becomes a terminal component means it will not call the next middleware component in the request processing pipeline.
Then the question that comes to your mind is how to call the next component in the request processing pipeline, the answer is registered your middleware component using the Use extension method as shown below.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Use(async (context, next) => { await context.Response.WriteAsync("Getting Response from 1st Middleware"); await next(); }); app.Run(async (context) => { await context.Response.WriteAsync("Getting Response from 2nd Middleware"); }); }Now run the application and you will see the output as expected which is coming from both the middleware components.
Understanding the Use extension method:
The Use extension method adds a middleware delegate defined in-line to the application’s request pipeline. Following is the definition of the Use extension method:
This method is also implemented as an extension method on the IApplicationBuilder interface. This is the reason why we are able to invoke this method using the IApplicationBuilder instance. As you can see from the above definition, this method takes two input parameters. The first parameter is the HttpContext context object through which it can access both the HTTP request and response. The second parameter is the Func type i.e. it is a generic delegate that can handle the request or call the next middleware component in the request pipeline.
That’s it for today. In the next article, I am going to discuss the ASP.NET Core request processing pipeline with an example. Here, in this article, I try to explain how to Configuring Middleware Components in the ASP.NET Core application to handle the request processing pipeline with an example.
Summary:
I hope this post will be helpful to understand the concept of Configuring Middleware Components in ASP.NET Core
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
We greatly appreciate your effort, Thank you For posting.
ReplyDeleteVisit us: dot net training
Visit us: Dot Net Online Training Hyderabad
Visit us: .net online training india
Visit us: Dot Net Training Online India
Visit us: .Net Online Training Hyderabad
Visit us: Dot Net Online Training
Visit us: Dot Net Online Course
Visit us: .Net Online Training