【发布时间】:2019-09-18 11:40:00
【问题描述】:
我正在尝试关注一些过时的 AspNet Core 1.1 教程项目 (当我使用 Visual Studio 2019 预览版 2.0 时, 和 .net core v3.0.0-preview9 已安装)
我已经有一个数据库,其中包含由 EntityFrameworkCore3.00-preview 创建的几个表, 它工作正常。
现在我正在尝试将自动化添加到项目中 所以我从 IdentityDbContext 派生出我的数据库上下文类,
public class MyDbContext : IdentityDbContext
而不是
public class WorldContext : DbContext
制作添加迁移和更新数据库,在数据库中我看到许多新表
此时我的应用程序可以启动,但在我添加一行的那一刻
services.AddIdentity<IdentityUser, IdentityRole>();
到 Startup.cs 中的 ConfigurationServices
应用程序崩溃并显示消息:
System.AggregateException H结果=0x80131500 消息=无法构造某些服务(验证服务描述符时出错'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity. IdentityUser]' 尝试激活“Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]”时:无法解析类型“Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft .AspNetCore.Identity.IdentityUser]'。)(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory2[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole]”时出错:无法解析类型“Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]”的服务。)(验证服务描述符“ServiceType: Mi”时出错crosoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]':无法解析“Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务.)(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser]”时出错:无法解析“Microsoft.AspNetCore.Identity.IUserStore@987654332”类型的服务@1[Microsoft.AspNetCore.Identity.IdentityUser]'。)(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]”时出错:无法解析服务对于类型“Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole]”。) Source=Microsoft.Extensions.DependencyInjection 堆栈跟踪: 在 Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) 在 Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() 在 Microsoft.Extensions.Hosting.HostBuilder.Build() 在 C:\Users\mli2805\source\repos\TheWorld\TheWorld\Program.cs:line 10 中的 TheWorld.Program.Main(String[] args) 处内部异常 1: InvalidOperationException:在尝试激活“Microsoft .AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'。
内部异常 2: InvalidOperationException:无法解析类型“Microsoft.AspNetCore.Identity.IUserStore
1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]”的服务。
如果我实施
public class MyUserStore : IUserStore<IdentityUser>
并添加一行
services.AddScoped<IUserStore<IdentityUser>, MyUserStore>();
到 ConfigurationServices,我收到关于
的下一个错误无法解析类型“Microsoft.AspNetCore.Identity.IRoleStore”的服务
等等,尽管在教程中没有关于实现所有这些接口的内容。 在我看来,我在配置服务中做错了什么?
【问题讨论】:
标签: c# asp.net-core asp.net-identity