Saturday, 10 August 2024

How to create and use middleware in asp.net core

Middleware is piece of code that's assembled into an app pipeline to handle requests and responses. 

  • Each middleware component in the request pipeline is responsible for invoking the next component in the pipeline or short-circuiting the pipeline. 
  • each component can perform work before and after the next component in the pipeline.

Request delegates are used to build the request pipeline.

Request delegates are configured using Run, Map, and Use extension methods. 

UseWhen also branches the request pipeline based on the result of the given predicate. Unlike with MapWhen, this (UseWhen) branch is rejoined to the main pipeline if it doesn't short-circuit or contain a terminal middleware.

app.UseWhen(context => context.Request.Query.ContainsKey("branch"),appBuilder => HandleBranchAndRejoin(appBuilder));

Custom Middleware: We can create custom middleware either by convention or impelementing IMiddle<T> interface.

Convention (Class) based Middleware: Following point need to remember while creating conventional middleware:

  • A public constructor with a parameter of type RequestDelegate.
  • A public method named Invoke or InvokeAsync. This method must return a Task and accept a first parameter of type HttpContext.
  • Additional parameters for the constructor and Invoke/InvokeAsync are populated by dependency injection (DI).

To use this middleware, we need to call USEMIDDLEWARE<CLSNAME>() extension method.

Note: Class based (Conventional) middleware has a singleton scope (application lifetime).

Per-request middleware dependencies

Middleware is constructed at app startup and therefore has application lifetime. Scoped lifetime services used by middleware constructors aren't shared with other dependency-injected types during each request. To share a scoped service between middleware and other types, add these services to the InvokeAsync method's signature. The InvokeAsync method can accept additional parameters that are populated by DI.

Factorybased Middleware

UseMiddleware extension methods check if a middleware's registered type implements IMiddleware. If it does, the IMiddlewareFactory instance registered in the container is used to resolve the IMiddleware implementation instead of using the convention-based middleware activation logic. The middleware is registered as a scoped or transient service in the app's service container.

IMiddleware is activated per client request (connection), so scoped services can be injected into the middleware's constructor.

IMiddleware defines middleware for the app's request pipeline. The InvokeAsync(HttpContext, RequestDelegate) method handles requests and returns a Task that represents the execution of the middleware.

USE: builder.Services.AddTransient<FactoryActivatedMiddleware>(); 

FactoryActivatedMiddleware is the name of class implements IMiddleware.


No comments:

Post a Comment

How to create and use middleware in asp.net core

Middleware is piece of code that's assembled into an app pipeline to handle requests and responses.  Each middleware component in the re...