In this section, we are going to understand new pipeline concept in ASP.NET Core which is Middleware. If you have been doing ASP.NET stuff since a while, you must be knowing that HTTP-Handlers and HTTP-Modules have always been part of ASP.NET processing structure. But, with new ASP.NET Core concept, Handlers and Modules are now history. They are no longer in use. Rather than that new Pipeline is implemented and that is called Middleware. Developers can utilize normal existing Middlewares or they can write their own custom middleware depending on the need. Below is the high level diagram of MiddleWare.
Here, once an HTTP request comes at the ASP.NET server, the server will delegate the request to the first middleware in our application. Each middleware has the option of creating a response, or calling next middleware. Basically, its a bi-directional pipeline. Therefore, if the first component passes the request to next middleware, first component will see response coming out of the other components. Each Middleware has got specific sets of functionalities. Now, let’s see how to configure Middleware. Let me create one empty project as shown below. Here, I am using Visual Studio 2017 RC Version. You can use VS-2015 as well.
Once the project got created; we will see something like below project structure.
Now, let’s go ahead and see the Startup file created. Below is the sample startup file given to us.
As you can see here, we have two methods here. ConfigureServices and Configure. ConfigureServices is the place where we Dependency injection happens and Configure is the place, where we use middleware. We will do deep dive around the same in a moment. Let’s first run the same and see how processing goes.
Obviously, first it will hit ConfigureServices to check any component has been requested, later on it will to pipeline section and execute the same in linear ordering.
Note:- Ordering of Midlleware is very important. We will explore this stuff in coming section.
After above execution, it simply produced Hello World text in browser.
Now, if you closely look at this Run method. This Run method accepts RequestDelegate as a parameter whose signature goes like it accepts one parameter as a HTTP Context and return type is Task something like shown below.
Now, what this middleware component is doing; its handling all the requests, made to the app and sending back the response. Run method adds a RequestDelegate which is terminal to the request pipeline. This means any middleware comes after this; won’t be reached. At the moment, we don’t have much in the middleware section. Let’s go ahead and add one. I am going to use here use. Below is the structure for that.
In the above snippet, I have added one middleware and in the response of that middleware, I have added some message to identify the same. Let’s go ahead and run the same. Here, it produced me the same Hello World output as shown below. Nothing changed.
This is why ordering of Middleware is very important. Run is a terminal middleware, which terminates the middleware chain, hence our new middleware couldn’t reach. Middlewares always execute from top to bottom. Hence, let’s place the same before Run method.
With the above change in place, when I go ahead and run the same,
I hope you would have liked this discussion around basics of Middleware. In the coming section, we will delve further into different methods of Middleware and see how this chaining actually works. Till then stay tuned and Happy Coding.
Thanks,
Rahul Sahay
Happy Coding