gpt4 book ai didi

c# - ASP.NET Core 2 - 身份 - 自定义角色的 DI 错误

转载 作者:可可西里 更新时间:2023-11-01 03:08:09 24 4
gpt4 key购买 nike

我在我的 Startup.cs 中得到了这段代码:

 services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

在同一个文件中,我还将 service.UseIdentity() 替换为 app.UseAuthentication();,这是 MS 在新版本的 ASP Core 2 中推荐的.

我的数据库上下文:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}


//public DbSet<ApplicationUser> ApplicationUser { get; set; }

//public DbSet<ApplicationRole> ApplicationRole { get; set; }
}

还有我的自定义 Role 类:

 public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }

public ApplicationRole(string roleName) : base(roleName) { }

public bool IsDefault { get; set; }
}

运行应用程序时,我得到了一个运行的 SeedDatabase 辅助方法:

var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>();

一切正常,但自从将 VS 2017 更新到最新版本并安装 .NET Core 2.0 后,最后一行代码现在抛出以下异常:

System.AggregateException occurred
HResult=0x80131500
Message=One or more errors occurred. (Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.RoleManager`1[CspLicensingPortal.Models.ApplicationRole]' from root provider.)
Source=<Cannot evaluate the exception source>
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at CspLicensingPortal.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in D:\gsandorx\Documents\Visual Studio 2017\Projects\CspLicensingPortal\CspLicensingPortal\Startup.cs:line 275

Inner Exception 1:
InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.RoleManager`1[MyApplication.Models.ApplicationRole]' from root provider.

我不确定为什么 DI 服务管理器不再能够找到我的 ApplicationRole 类。我已经检查过,我所有的引用都使用了这个类,而不是默认的 IdentityRole。

有什么想法吗?

最佳答案

您必须自己创建 IServiceScope。

为此你必须更换

var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>();

using (IServiceScope scope = app.ApplicationServices.CreateScope()) {
RoleManager<IdentityRole> roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();

// Seed database code goes here
}

关于c# - ASP.NET Core 2 - 身份 - 自定义角色的 DI 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805561/

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