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.

Solution: 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.

Edit this Article
.net core asp.net core mvc asp.net core tutorial Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

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

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy