Middleware in asp.net core 2.2 (Part 3)


As the name suggest middleware means it will work as mediator between request and response. It is pieces of code that handle the requests and responses. They also chained each other to form the pipeline. Any incoming request are passed through the pipeline where each middleware has a chance to do some task before passing to next middleware in the pipeline

Outgoing response also passed through the same pipeline in reversed order as given below image.

Middleware can do all type of task like handling authentication, errors, static files, MVC etc. in ASP.NET Core.

1. In middleware request can pass from all middleware in pipeline.
2. There could be also Short-circuiting in middleware.
3. There could be also Branching the pipeline in middleware
4. There could be also Rejoining of branch in middleware.
5. There could be also Non-Rejoining of branch in middleware.

How to configure pipeline
We typically configure the asp.net pipeline in the Configure method of Startup class by calling Use* methods on the IApplicationBuilder. As given below image

In the above image, we saw that each use* method is using to add the middleware in pipeline. In the above example Request will pass through DeveloperExceptionPage –> StaticsFiles –> Authentication
–> MVCWithDefaultRoute

Again response will pass through MVCWithDefaultRoute–> Authentication–> StaticsFiles
–> DeveloperExceptionPage middleware.

Use and Run are the extension method to add the middleware to the pipeline. Run is used to add a terminal middleware which will execute at last of the pipeline.

Now we will see the basic example of request and response flow where request will pass from start middleware to end terminal middleware and response will pass from end terminal middleware to Start middleware.

Basis Pipeline Example

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;

namespace MiddleWare_Sample
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

            //Middleware A
            app.Use(async (context, next) =>
            {
                Debug.WriteLine("A (before)");
                await next();
                Debug.WriteLine("A (after)");
            });

            // Middleware B
            app.Use(async (context, next) =>
            {
                Debug.WriteLine("B (before)");
                await next();
                Debug.WriteLine("B (after)");
            });

            // Middleware C (terminal)
            app.Run(async context =>
            {
                Debug.WriteLine("C (end MiddleWare in pipeline)");
                await context.Response.WriteAsync("Hello world");
            });

        }
    }
}

In the above example we saw the basic middleware without any condition. But we can design the middleware on basis of our requirement. In next post i will share the remaining the middleware with example. Thanks for reading.