Saturday, October 27, 2018

.Net Core | Http Request Pipeline & Middleware

In .Net Core web application initialization and startup has been totally changed.

Configure Method of Startup class configures Http Request pipeline.

  • Global.asax has been removed. 
  • ini, xml and json files taken place to use in configuration 


Middleware

Middleware is software that's assembled into an app pipeline to handle requests and responses.
  • A Middleware can choose whether the request has to pass to next component or it can terminate the chain that is called Short-Circuiting the pipeline. Middleware3 is doing it in below example.
  • Middleware can perform work before calling next component and can perform work after the next component call finishes.




Run, Map and Use methods 


Run: First occurrence of Run terminates the executor branch or request pipeline if no branches. 



public class Startup
{
        public void Configure(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                await context.Response.WriteAsync("Hello megabyteland.blogspot.com");
            });
        }
}

Use: To configure multiple middlewares you need to use Use method. Second parameter of Use is delegate for next middleware defined in the pipeline.
public class Startup
{
  public void Configure(IApplicationBuilder app)
        {
            app.Use(async (context, next) =>
            {
                // Do work before invoking next middleware.
                await next.Invoke();
                // Do work after invoking next middleware.
            });

            app.Run(async context =>
            {
                await context.Response.WriteAsync(("Hello megabyteland.blogspot.com");
            });
        }
}



Map: If you want to create middlewares in separate methods(branch) then use Map method.
public class Startup
{
    private static void HandleMap1(IApplicationBuilder app)
    {
        app.Run(async context =>
        {
            await context.Response.WriteAsync("Map 1");
        });
    }

    private static void HandleMap2(IApplicationBuilder app)
    {
        app.Run(async context =>
        {
            await context.Response.WriteAsync("Map 2");
        });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Map("/map1", HandleMap1);  // Mapped delegate

        app.Map("/map2", HandleMap2);  // Mapped delegate

        app.Run(async context =>          //non-mapped delegate 
        {
            await context.Response.WriteAsync("Hello from non-mapped delegate.");
        });
    }
}


Creating your own middleware


Below given code is template kind a code to create middlewares

For example you want to create a middleware to authenticate from Active Directory of your company, to identify the user is belongs to your company.

using
 System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace AdAuthentication
{
    public class RequestAdAuthenticationMiddleware
    {
        private readonly RequestDelegate _next;

        public RequestAdAuthenticationMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            //Write here Ad Authentication logic

            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }
    }
}

Add Builder namespace to use IApplicationBuilder interface  

using Microsoft.AspNetCore.Builder;
namespace AdAuthentication
{
    public static class RequestAdAuthenticationMiddlewareExtensions
    {
        public static IApplicationBuilder UseAdAuthentication (
            this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RequestAdAuthenticationMiddleware>();
        }
    }
}

  • Add AdAuthentication namespace in Startup.cs class
    using AdAuthentication;
  • Call middleware UseAdAuthentication
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            ::
            ::
            app.UseAdAuthentication();
            ::
            ::
}


Built-in Middlewares 
Below given middlewares are available in .Net Core. Calling order of a middleware is very important for below reasons.
  • To work Middleware properly 
  • A middle ware can be a terminal of HttpRequest Pipeline
Consider thinking about calling order, before calling any in-built or custom middleware.

Middleware
Description of Middleware
Order To Call Middleware
Provides authentication support.
Before HttpContext.User is needed. Terminal for OAuth callbacks.
Tracks consent from users for storing personal information and enforces minimum standards for cookie fields, such as secure and SameSite.
Before middleware that issues cookies. Examples: Authentication, Session, MVC (TempData).
Configures Cross-Origin Resource Sharing.
Before components that use CORS.
Configures diagnostics.
Before components that generate errors.
Forwards proxied headers onto the current request.
Before components that consume the updated fields. Examples: scheme, host, client IP, method.
Allows an incoming POST request to override the method.
Before components that consume the updated method.
Redirect all HTTP requests to HTTPS (ASP.NET Core 2.1 or later).
Before components that consume the URL.
Security enhancement middleware that adds a special response header (ASP.NET Core 2.1 or later).
Before responses are sent and after components that modify requests. Examples: Forwarded Headers, URL Rewriting.
Processes requests with MVC/Razor Pages (ASP.NET Core 2.0 or later).
Terminal if a request matches a route.
Interop with OWIN-based apps, servers, and middleware.
Terminal if the OWIN Middleware fully processes the request.
Provides support for caching responses.
Before components that require caching.
Provides support for compressing responses.
Before components that require compression.
Provides localization support.
Before localization sensitive components.
Defines and constrains request routes.
Terminal for matching routes.
Provides support for managing user sessions.
Before components that require Session.
Provides support for serving static files and directory browsing.
Terminal if a request matches a file.
Provides support for rewriting URLs and redirecting requests.
Before components that consume the URL.
Enables the WebSockets protocol.
Before components that are required to accept WebSocket requests.




















































No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...