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

时间:2018-09-24 22:06:48

标签: c# asp.net-core asp.net-identity identityserver4

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

 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(Type   serviceType,ServiceProvider serviceProvider)   Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type   服务类型)   IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder   应用程序)在MongoDbStartup.cs

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

IdentityServerSample.Startup.Configure(IApplicationBuilder应用,   Startup.cs中的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.ApplicationInsightsStartupFilter + <> 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()

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

1 个答案:

答案 0 :(得分:5)

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

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
}