简体   繁体   中英

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

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
}

If you're using provided Asp.netcore Identity (AppIdentityDbContext), you can try changing validateScope to production if you were in Development, in Program.cs:

webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseIISIntegration();
                    webBuilder.UseDefaultServiceProvider((context, options) =>
                    {
                        options.ValidateScopes = context.HostingEnvironment.IsProduction();
                    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM