gpt4 book ai didi

c# - 无法从根提供程序解析范围服务 'Microsoft.AspNetCore.Identity.UserManager` 1[IdentityServerSample.Models.ApplicationUser]'

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:12 25 4
gpt4 key购买 nike

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

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



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

最佳答案

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

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
}

关于c# - 无法从根提供程序解析范围服务 'Microsoft.AspNetCore.Identity.UserManager` 1[IdentityServerSample.Models.ApplicationUser]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52487989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com