✅ 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
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
I'm not sure, which part are you confused about?
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
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
i have two interfaces IMiddlewear and IMiddlewearFactory
IMiddlewearFactory is registers as a scoped
while the IMiddleWear is can be registerd as transiant or scoped
Yep, that's right
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
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
yes i am still learning both of them
Personally I favor the factory backed one for sure, the structured approach over convention is really decent
dose this means that i should learn only the facory based ?
I think it's worth knowing about all of them at least
ok thank you very much
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
is it hard to pass the dependancies to the convinional middlewear
You can always get them through the RequestServices on the HttpContext
i can found this on this link of the docs https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-8.0#per-request-middleware-dependencies
Write custom ASP.NET Core middleware
Learn how to write custom ASP.NET Core middleware.