December 2019
Intermediate to advanced
510 pages
11h 33m
English
As discussed earlier, middleware can instantiate dependencies through a dependency injection container. We should take the life cycle of the middleware into consideration: they are initialized once per application lifetime. As a consequence, if we try to consume a scoped or transient instance into our middleware we shouldn't inject them through the constructor of the middleware, because this will cause some dependency resolution issues. A good way to avoid this is to use the parameter injection in the Invoke or InvokeAsync methods:
namespace Middleware { public class MyMiddleware { readonly RequestDelegate _next; public MyMiddleware(RequestDelegate next) { _next = next; } public async Task