✅ Factory-based middleware activation in asp.net core

this part of the docs says that Factory-based middleware is activated per request while when i register this middleware in app service container it is registered as a transient or scoped , this part confuse me is it registered twice https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-8.0
IMiddlewareFactory/IMiddleware is an extensibility point for middleware activation that offers the following benefits:

Activation per client request (injection of scoped services)
Strong typing of 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.
IMiddlewareFactory/IMiddleware is an extensibility point for middleware activation that offers the following benefits:

Activation per client request (injection of scoped services)
Strong typing of 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.
Factory-based middleware activation in ASP.NET Core
Learn how to use strongly-typed middleware with a factory-based activation implementation in ASP.NET Core.
16 Replies
Sossenbinder
Sossenbinder5mo ago
I'm not sure, which part are you confused about?
steven preadly
steven preadlyOP5mo ago
Activation per client request (injection of scoped services) then i use this middlewear in the program.cs i added it as a transiant while it is already registred as a scoped how come this happiens
Sossenbinder
Sossenbinder5mo ago
Right, so the middleware factory creates your middleware in the context of a request, and I'm pretty sure it will receive an IServiceProvider which is already scoped to the request Usually your middleware would only be created once per request, so whether it ends up transient or scoped does not make a huge difference But since your Middleware is resolved from a scoped factory, it will also receive the services from that scope
steven preadly
steven preadlyOP5mo ago
i have two interfaces IMiddlewear and IMiddlewearFactory IMiddlewearFactory is registers as a scoped while the IMiddleWear is can be registerd as transiant or scoped
Sossenbinder
Sossenbinder5mo ago
Yep, that's right
steven preadly
steven preadlyOP5mo ago
when middleware implements the IMiddlewear it used the IMiddlewearFacory interface to create an instance of the middleware and IMiddlewearFacory itself is registerd as a scoped sevice in the DI What makes it easy to inject scoped service in the middlewear that implements the IMiddlewear
Sossenbinder
Sossenbinder5mo ago
The default IMiddlewareFactory captures the DI serviceprovider and builds your middleware from there, this would be something you have to do on your own for convention based middleware So you can just ask for scoped dependencies in your constructor, instead of having to resolve them from the context's services Note that this is only about the default IMiddlewareFactory You can technically register your own
steven preadly
steven preadlyOP5mo ago
yes i am still learning both of them
Sossenbinder
Sossenbinder5mo ago
Personally I favor the factory backed one for sure, the structured approach over convention is really decent
steven preadly
steven preadlyOP5mo ago
dose this means that i should learn only the facory based ?
Sossenbinder
Sossenbinder5mo ago
I think it's worth knowing about all of them at least
steven preadly
steven preadlyOP5mo ago
ok thank you very much
Sossenbinder
Sossenbinder5mo ago
I'd say the convention based middleware might be decent when you don't need any dependencies, but 99% of the time a middleware will have something to do with your request, so you'd probably almost always want to have some scoped dependencies, and that's where the factory based approach is really neat
steven preadly
steven preadlyOP5mo ago
is it hard to pass the dependancies to the convinional middlewear
Sossenbinder
Sossenbinder5mo ago
You can always get them through the RequestServices on the HttpContext
Want results from more Discord servers?
Add your server