Cannot resolve scoped service from root provider ASP.NET Core

Today in this article, we will cover below aspects,

Issue Description

.NET Core runtime gives error,

System.InvalidOperationException- Cannot resolve scoped service ” from root provider.

Resolution

The error seems could occur at multiple places depending on your specific implementation while using the services in your code base.

As we know one can register the Services as Singleton or Scoped or Transient.

While scope service instances are created once per request it is often recommended they are resolved using a scoped instance of Application Builder. Then followed by using a service provider one can resolve the dependencies.

You might need to consider the below aspects also before resolving the issue,

  • How a lifetime of service instances is registered.
  • How the instance is initialized.
  • How the instance is resolved.

I was able to resolve the issue using the scoped service provider instance using the below code base. One can take a similar approach to resolve their specific issues.

var scope = app.ApplicationServices.CreateScope();
var service = scope.ServiceProvider.GetService<IUniqueIdService>();

or you can use using the scope for the required DI as below,

If you need to resolve services within ConfigureServices() method then one can use the below approach too to resolve the instances,

  services.AddSingleton(provider => new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new SourceMappingProfile(provider.CreateScope().ServiceProvider.GetService<IEmployeeType>()));
            }).CreateMapper());

That’s All!

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



13 thoughts on “Cannot resolve scoped service from root provider ASP.NET Core

  1. Thank you !
    This works for me !
    I use hangfire, I’m running successfully .net core 3.1, but my new project is .net 6.0
    3.1 provide in startup.cs Configure area but .net 6 just provides Program.cs
    I was generated serviceProvider from IServiceProvider in 3.1 but I can’t generate in 6.0 in IServiceProvider, I think manually created provider can’t assign DbContext in my project.
    This solution ;
    //Create application
    var app = builder.Build();
    //Create Service Provider
    var serviceProvider = app.Services;
    //And last one create Service Provider’s scope.
    var serviceScope = serviceProvider.CreateScope();

    If I was not create scope i will getting this error ;
    ‘Cannot resolve ‘Business.Abstract.IJobService’ from root provider because it requires scoped service ‘DataAccess.Concrete.EntityFramework.Context.PostgreDbContext’.’

  2. This helped me but I don’t know why.

    // ERROR
    Utilities.ConfigureAccountManager(app.ApplicationServices.GetService());

    // WORKS
    var scope = app.ApplicationServices.CreateScope();
    Utilities.ConfigureAccountManager(scope.ServiceProvider.GetService());

Leave a Reply

Your email address will not be published. Required fields are marked *