我正在尝试为我的身份模型实现自定义UserStore
;但是,我在应用程序启动时收到以下运行时错误:
无效操作异常: 无法解析类型的服务 '[项目]。Models.Identity.ApplicationUserStore' 同时尝试 激活"[项目]。Models.Identity.ApplicationUserManager'.
叠:
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type 服务类型, 类型实现类型, 调用站点链 调用站点链, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type 服务类型,类型实现类型,调用站点链调用站点链) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor 描述符, 类型 serviceType, CallSiteChain callSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type 服务类型,调用站点链 调用站点链) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type 服务类型,调用站点链 调用站点链) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType) System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd(TKey key, Func valueFactory) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type 服务类型) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供程序,类型服务类型) Microsoft.AspNetCore.Identity.IdentityBuilder+<>c__DisplayClass22_0.b__0(IServiceProvider services) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite, TArgument argument) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite, TArgument argument) Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.b__0(ServiceProviderEngineScope 范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type 服务类型) Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, 类型类型, 类型需要由, bool is默认参数必需) lambda_method(Closure , IServiceProvider , object[] ) Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass4_0.b__0(ControllerContext 控制器上下文) Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.g__CreateController|0(ControllerContext 控制器上下文) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref 状态下一个,引用范围范围,引用对象状态,引用布尔值已完成) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref 状态下一个,引用范围范围,引用对象状态,引用布尔值已完成) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
我的UserStore
实现:
public class ApplicationUserStore : IUserStore<Employee>,
IUserClaimStore<Employee>,
IUserLoginStore<Employee>,
IUserRoleStore<Employee>,
IUserPasswordStore<Employee>,
IUserSecurityStampStore<Employee>
{
#region Constructor signatures I tried for dependency injection
public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
{
}
public ApplicationUserStore(ApplicationDbContext context)
{
}
public ApplicationUserStore()
{
}
public ApplicationUserStore(DbContext context)
{
}
#endregion
...
我的UserManager
实现:
public class ApplicationUserManager : UserManager<Employee>
{
public ApplicationUserManager(ApplicationUserStore store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<Employee> passwordHasher, IEnumerable<IUserValidator<Employee>> userValidators, IEnumerable<IPasswordValidator<Employee>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<Employee>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
}
我的SignInManager
实现:
public class ApplicationSignInManager : SignInManager<Employee>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<Employee> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<Employee>> logger, IAuthenticationSchemeProvider schemes)
: base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
{
}
}
并且,在Startup.ConfigureServices
:
...
services.AddIdentityCore<Employee>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddSignInManager<ApplicationSignInManager>()
.AddDefaultTokenProviders();
services.AddTransient<IUserStore<Employee>, ApplicationUserStore>();
...
我需要知道导致问题的原因,如何解决它,以及是否有 Asp.Net Core 2.1依赖注入激活器签名的文档。
在这里和这里查看了开源实现之后,我想出了问题可能是什么的线索 - 我猜对了。UserStore
需要在Startup.ConfigureServices
中通过此行注册:
services.AddScoped<ApplicationUserStore>();
所以,我的最终方法是:
...
services.AddIdentityCore<Employee>()
.AddUserManager<ApplicationUserManager>()
.AddSignInManager<ApplicationSignInManager>()
.AddDefaultTokenProviders();
services.AddScoped<IUserStore<Employee>, ApplicationUserStore>();
services.AddScoped<ApplicationUserStore>();
...
即使,它没有工作:
services.AddScoped<IUserStore<Employee>, ApplicationUserStore>();