首页 > 解决方案 > ASP.NET Core v1.1 中的 IdentityServer 和 Simple Injector 集成

问题描述

我们为我们的应用程序组件使用 Simple Injector,我们需要访问 IdentityServer 接口实现 ( IResourceOwnerPasswordValidator) 内部的组件,该接口实现本身就在内部IServiceCollection。如果我理解正确,我们需要实现ASP.NET Core MVC 集成 4.0-4.5并使用container.CrossWire<IResourceOwnerPasswordValidator>(app)(而不是AutoCrossWireAspNetComponents()因为我们在 ASP.NET Core 1.1 项目中以 net452 为目标)。当我在尝试此实现后尝试注入构造函数时,在验证期间出现此错误ISignInManagerResourceOwnerPasswordValidator

配置无效。创建 IResourceOwnerPasswordValidator 类型的实例失败。IResourceOwnerPasswordValidator 类型的注册委托引发了异常。尝试激活“ResourceOwnerPasswordValidator”时无法解析“ISignInManager”类型的服务。

完全例外:

{System.InvalidOperationException: The configuration is invalid. Creating the instance for type IResourceOwnerPasswordValidator failed. The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'. ---> SimpleInjector.ActivationException: The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'. ---> System.InvalidOperationException: Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)
   at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary`2 dictionary, TKey key, Func`3 valueFactory, TArg arg)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at lambda_method(Closure )
   at lambda_method(Closure )
   at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
   at SimpleInjector.InstanceProducer.GetInstance()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.GetInstance()
   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
   at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify, Scope verificationScope)
   at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
   at SimpleInjector.Container.Verify()
   at Authentication.Web.UI.IoCBootstrapper.InitializeContainer(IApplicationBuilder app, IHostingEnvironment env) in D:\IoCBootstrapper.cs:line 110
   at Authentication.Web.UI.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery, AppSettings appSettings) in D:\Startup.cs:line 132}
    [System.InvalidOperationException]: {System.InvalidOperationException: The configuration is invalid. Creating the instance for type IResourceOwnerPasswordValidator failed. The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'. ---> SimpleInjector.ActivationException: The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'. ---> System.InvalidOperationException: Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.Re
sourceOwnerPasswordValidator'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)
   at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary`2 dictionary, TKey key, Func`3 valueFactory, TArg arg)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at lambda_method(Closure )
   at lambda_method(Closure )
   at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
   at SimpleInjector.InstanceProducer.GetInstance()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.GetInstance()
   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
   at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify, Scope verificationScope)
   at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
   at SimpleInjector.Container.Verify()
   at Authentication.Web.UI.IoCBootstrapper.InitializeContainer(IApplicationBuilder app, IHostingEnvironment env) in D:\IoCBootstrapper.cs:line 110
   at Authentication.Web.UI.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery, AppSettings appSettings) in D:\Startup.cs:line 132}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2146233079
    InnerException: {SimpleInjector.ActivationException: The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'. ---> System.InvalidOperationException: Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet`1 callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)
   at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary`2 dictionary, TKey key, Func`3 valueFactory, TArg arg)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at lambda_method(Closure )
   at lambda_method(Closure )
   at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
   at SimpleInjector.InstanceProducer.GetInstance()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.GetInstance()
   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()}
    Message: "The configuration is invalid. Creating the instance for type IResourceOwnerPasswordValidator failed. The registered delegate for type IResourceOwnerPasswordValidator threw an exception. Unable to resolve service for type 'Authentication.Common.Interfaces.SignIn.ISignInManager' while attempting to activate 'Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator'."
    Source: "SimpleInjector"
    StackTrace: "   at SimpleInjector.InstanceProducer.VerifyInstanceCreation()\r\n  
    at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify, Scope verificationScope)\r\n  
    at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)\r\n  
    at SimpleInjector.Container.Verify()\r\n  
    at Authentication.Web.UI.IoCBootstrapper.InitializeContainer(IApplicationBuilder app, IHostingEnvironment env) in D:\\IoCBootstrapper.cs:line 110\r\n  
    at Authentication.Web.UI.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery, AppSettings appSettings) in D:\\Startup.cs:line 132"
    TargetSite: {System.Object VerifyInstanceCreation()}

我怎样才能做到这一点?如何将 Simple Injector 注册注入 ASP.NET DI 注册?

注册:

container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
services.AddSingleton<IControllerActivator>(
    new SimpleInjectorControllerActivator(container));
services.AddSingleton<IViewComponentActivator>(
    new SimpleInjectorViewComponentActivator(container));
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);

container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);

container.Register<ISignInManager, SignInManager>(Lifestyle.Transient);
..
//other registrations that used by SignInManager
..

container.CrossWire<IResourceOwnerPasswordValidator>(app);

我们正在使用:

标签: asp.net-core-mvc.net-4.5identityserver4simple-injectorasp.net-core-1.1

解决方案


异常状态:

尝试激活“Authentication.Web.UI.IdentityServer.ResourceOwnerPasswordValidator”时无法解析“Authentication.Common.Interfaces.SignIn.ISignInManager”类型的服务。

这意味着ResourceOwnerPasswordValidator依赖ISignInManager但没有ISignInManager在 ASP.NET Core 配置系统中注册。


推荐阅读