Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[IdentityServerSample.Models.ApplicationUser]' from root provider

i am extending the identity server to use custom identity server implementation

 public static void UseMongoDbForIdentityServer(this IApplicationBuilder app)
    {

        //Resolve Repository with ASP .NET Core DI help 
        var repository = (IRepository)app.ApplicationServices.GetService(typeof(IRepository));

        //Resolve ASP .NET Core Identity with DI help
        var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));

        // --- Configure Classes to ignore Extra Elements (e.g. _Id) when deserializing ---
        ConfigureMongoDriver2IgnoreExtraElements();

        var createdNewRepository = false;

        ...
}

this is how my startup file looks like

      public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<ConfigurationOptions>(Configuration);
     ...

        services.AddIdentityServer(
                  options =>
                  {
                      options.Events.RaiseSuccessEvents = true;
                      options.Events.RaiseFailureEvents = true;
                      options.Events.RaiseErrorEvents = true;
                  }
              )
              .AddMongoRepository()
              .AddMongoDbForAspIdentity<ApplicationUser, IdentityRole>(Configuration)
              .AddClients()
              .AddIdentityApiResources()
              .AddPersistedGrants()
            .AddDeveloperSigningCredential();
      ...

    }

And this is the error i am getting

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder app) in MongoDbStartup.cs

   var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));

IdentityServerSample.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs

            app.UseMongoDbForIdentityServer();

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app) Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

I know there are similar questions asked, but none seems to solve my problem

like image 576
Obed Amoasi Avatar asked Sep 24 '18 22:09

Obed Amoasi


People also ask

Can I resolve a scoped service from the root provider?

As such, it cannot resolve services that should only be consumed within service scopes. If you want to resolve a scoped service from the root provider, for example when you are consuming it from a singleton service, you should create a service scope first using the IServiceScopeFactory:

What is system invalidoperationexception-cannot resolve scoped service?

System.InvalidOperationException- Cannot resolve scoped service ” from root provider. 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.

How to get non root service provider from iservicescopefactory?

Try resolve IServiceScopeFactory first and then call CreateScope () to get the non root service provider. You will need to store the factory somewhere to be able to dispose it. Thank you for response. You got the point with IServiceScopeFactory :) Hi I'm having the same issue.

How to resolve service instances created once per request are resolved?

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 below aspects also before resolving the issue. How a lifetime of service instances is registered.


1 Answers

You need a scope to resolve dependencies registered as scoped. To create it you can use the following:

using(var scope = app.ApplicationServices.CreateScope())
{
    //Resolve ASP .NET Core Identity with DI help
    var userManager = (UserManager<ApplicationUser>)scope.ServiceProvider.GetService(typeof(UserManager<ApplicationUser>));
    // do you things here
}
like image 108
Alex Riabov Avatar answered Oct 05 '22 20:10

Alex Riabov