You registered the IEmailRepository
as a scoped service, in the Startup
class.
This means that you can not inject it as a constructor parameter in Middleware
because only Singleton
services can be resolved by constructor injection in Middleware
. You should move the dependency to the Invoke
method like this:
public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context, IEmailRepository emailRepository)
{
try
{
await _next.Invoke(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex, emailRepository);
}
}