我正在尝试为我的身份模型实施自定义UserStore
;但是,我在应用启动时遇到了这个运行时错误:
InvalidOperationException:无法解析类型的服务 ' [项目] .Models.Identity.ApplicationUserStore'在尝试时 激活' [Project] .Models.Identity.ApplicationUserManager'。
堆栈:
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型 serviceType,类型implementationType,CallSiteChain callSiteChain, ParameterInfo []参数,bool throwIfCallSiteNotFound) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(类型 serviceType,Type implementationType,CallSiteChain callSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor 描述符,类型serviceType,CallSiteChain callSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(类型 serviceType,CallSiteChain callSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(类型 serviceType,CallSiteChain callSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(类型 serviceType)System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd(TKey key,Func valueFactory) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 serviceType,ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider 提供者,类型serviceType) Microsoft.AspNetCore.Identity.IdentityBuilder +<> c__DisplayClass22_0.b__0(IServiceProvider服务) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,TArgument参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,TArgument参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine + LT;> c__DisplayClass1_0.b__0(ServiceProviderEngineScope 范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 serviceType,ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型) Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(的IServiceProvider sp,类型类型,类型requiredBy,bool isDefaultParameterRequired) lambda_method(Closure,IServiceProvider,object []) Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider + LT;> c__DisplayClass4_0.b__0(ControllerContext controllerContext) Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider + LT;> c__DisplayClass5_0.g__CreateController | 0(ControllerContext controllerContext) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(REF 接下来的状态,ref范围,ref对象状态,ref bool isCompleted) 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 接下来的状态,ref范围,ref对象状态,ref bool isCompleted) 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依赖注入激活器签名是否存在任何文档。
答案 0 :(得分:2)
在审核了开源实施here和here之后,我想出了问题可能是什么 - 我猜对了。 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>();