Application startup exception: System.InvalidOperationException: Cannot resolve scoped service from root provider

Error: Application startup exception: System.InvalidOperationException: Cannot resolve scoped service from root provider.


Please Login to see the rest of the answer

Answer: There is a chance that you are trying to inject a Service into an Asp.Net Core 3.1 Middleware through a constructor. Instead of injecting a Service Interface through a Middleware Constructor, inject it through an invoke method/function like so:

 public async Task Invoke(HttpContext context, IAnalytics analytics)
        {

//Then use your Service in here

analytics.SomeFunction();

}
  • The service will be available to the Application when the Service Container is loaded.

    Let me know if this solves your problem by leaving me a review below: You can comment without creating an account or just like this article is this helped solve your problem.

Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy