AspNetCore.Docs/aspnetcore/fundamentals/middleware/extensibility.md

7.4 KiB

title author description monikerRange ms.author ms.custom ms.date uid
Factory-based middleware activation in ASP.NET Core guardrex Learn how to use strongly-typed middleware with a factory-based activation implementation in ASP.NET Core. >= aspnetcore-2.1 riande mvc 09/22/2019 fundamentals/middleware/extensibility

Factory-based middleware activation in ASP.NET Core

By Luke Latham

::: moniker range=">= aspnetcore-3.0"

xref:Microsoft.AspNetCore.Http.IMiddlewareFactory/xref:Microsoft.AspNetCore.Http.IMiddleware is an extensibility point for middleware activation.

xref:Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware* extension methods check if a middleware's registered type implements xref:Microsoft.AspNetCore.Http.IMiddleware. If it does, the xref:Microsoft.AspNetCore.Http.IMiddlewareFactory instance registered in the container is used to resolve the xref:Microsoft.AspNetCore.Http.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.

Benefits:

  • Activation per client request (injection of scoped services)
  • Strong typing of middleware

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

View or download sample code (how to download)

IMiddleware

xref:Microsoft.AspNetCore.Http.IMiddleware defines middleware for the app's request pipeline. The InvokeAsync(HttpContext, RequestDelegate) method handles requests and returns a xref:System.Threading.Tasks.Task that represents the execution of the middleware.

Middleware activated by convention:

[!code-csharp]

Middleware activated by xref:Microsoft.AspNetCore.Http.MiddlewareFactory:

[!code-csharp]

Extensions are created for the middlewares:

[!code-csharp]

It isn't possible to pass objects to the factory-activated middleware with xref:Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware*:

public static IApplicationBuilder UseFactoryActivatedMiddleware(
    this IApplicationBuilder builder, bool option)
{
    // Passing 'option' as an argument throws a NotSupportedException at runtime.
    return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}

The factory-activated middleware is added to the built-in container in Startup.ConfigureServices:

[!code-csharp]

Both middlewares are registered in the request processing pipeline in Startup.Configure:

[!code-csharp]

IMiddlewareFactory

xref:Microsoft.AspNetCore.Http.IMiddlewareFactory provides methods to create middleware. The middleware factory implementation is registered in the container as a scoped service.

The default xref:Microsoft.AspNetCore.Http.IMiddlewareFactory implementation, xref:Microsoft.AspNetCore.Http.MiddlewareFactory, is found in the Microsoft.AspNetCore.Http package.

::: moniker-end

::: moniker range="< aspnetcore-3.0"

xref:Microsoft.AspNetCore.Http.IMiddlewareFactory/xref:Microsoft.AspNetCore.Http.IMiddleware is an extensibility point for middleware activation.

xref:Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware* extension methods check if a middleware's registered type implements xref:Microsoft.AspNetCore.Http.IMiddleware. If it does, the xref:Microsoft.AspNetCore.Http.IMiddlewareFactory instance registered in the container is used to resolve the xref:Microsoft.AspNetCore.Http.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.

Benefits:

  • Activation per client request (injection of scoped services)
  • Strong typing of middleware

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

View or download sample code (how to download)

IMiddleware

xref:Microsoft.AspNetCore.Http.IMiddleware defines middleware for the app's request pipeline. The InvokeAsync(HttpContext, RequestDelegate) method handles requests and returns a xref:System.Threading.Tasks.Task that represents the execution of the middleware.

Middleware activated by convention:

[!code-csharp]

Middleware activated by xref:Microsoft.AspNetCore.Http.MiddlewareFactory:

[!code-csharp]

Extensions are created for the middlewares:

[!code-csharp]

It isn't possible to pass objects to the factory-activated middleware with xref:Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware*:

public static IApplicationBuilder UseFactoryActivatedMiddleware(
    this IApplicationBuilder builder, bool option)
{
    // Passing 'option' as an argument throws a NotSupportedException at runtime.
    return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}

The factory-activated middleware is added to the built-in container in Startup.ConfigureServices:

[!code-csharp]

Both middlewares are registered in the request processing pipeline in Startup.Configure:

[!code-csharp]

IMiddlewareFactory

xref:Microsoft.AspNetCore.Http.IMiddlewareFactory provides methods to create middleware. The middleware factory implementation is registered in the container as a scoped service.

The default xref:Microsoft.AspNetCore.Http.IMiddlewareFactory implementation, xref:Microsoft.AspNetCore.Http.MiddlewareFactory, is found in the Microsoft.AspNetCore.Http package.

::: moniker-end

Additional resources