繁体   English   中英

无法从根提供程序解析作用域服务“Microsoft.AspNetCore.Identity.UserManager`1[IdentityServerSample.Models.ApplicationUser]”

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

我正在扩展身份服务器以使用自定义身份服务器实现

 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;

        ...
}

这是我的启动文件的样子

      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();
      ...

    }

这是我得到的错误

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(类型 serviceType,ServiceProvider 服务提供商) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 serviceType)

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

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

 app.UseMongoDbForIdentityServer();

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder 应用程序) Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder 生成器) Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStart< >c__DisplayClass0_0.b__0(IApplicationBuilder 应用)Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.b__0(IApplicationBuilder 应用)Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder 构建器)Microsoft.AspNetCore.Hosting。 Internal.WebHost.BuildApplication()

我知道有类似的问题被问到,但似乎没有一个能解决我的问题

您需要一个作用域来解决注册为作用域的依赖项。 要创建它,可以使用以下命令:

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
}

如果您正在使用提供的 Asp.netcore 身份 (AppIdentityDbContext),如果您处于开发阶段,则可以尝试将 validateScope 更改为生产,在 Program.cs 中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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