Cannot resolve scoped service from root provider .Net Core 2

后端 未结 4 676
心在旅途
心在旅途 2020-12-07 19:45

When I try to run my app I get the error

InvalidOperationException: Cannot resolve \'API.Domain.Data.Repositories.IEmailRepository\' from root provider beca         


        
4条回答
  •  北海茫月
    2020-12-07 20:12

    Middleware is always a singleton so you can't have scoped dependencies as constructor dependencies in the constructor of your middleware.

    Middleware supports method injection on the Invoke method,so you can just add the IEmailRepository emailRepository as a parameter to that method and it will be injected there and will be fine as scoped.

    public async Task Invoke(HttpContext context, IEmailRepository emailRepository)
    {
    
        ....
    }
    

提交回复
热议问题