Мы используем Simple Injector для наших компонентов приложения, и нам нужен доступ к компоненту внутри реализации интерфейса IdentityServer (IResourceOwnerPasswordValidator
), которая сама находится внутри IServiceCollection
. Если я правильно понимаю, нам нужно реализовать ASP. NET Core MVC интеграция 4.0-4.5 и использовать container.CrossWire<IResourceOwnerPasswordValidator>(app)
(вместо AutoCrossWireAspNetComponents()
, поскольку мы нацеливаем net452 в ASP. NET Core 1.1 проекта). Когда я пытаюсь внедрить ISignInManager
в ResourceOwnerPasswordValidator
конструктор после попытки этой реализации, я получаю эту ошибку во время проверки:
Конфигурация недопустима. Не удалось создать экземпляр для типа IResourceOwnerPasswordValidator. Зарегистрированный делегат для типа IResourceOwnerPasswordValidator вызвал исключение. Не удалось разрешить службу для типа 'ISignInManager' при попытке активировать 'ResourceOwnerPasswordValidator'.
Полное исключение:
{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()}
Как мне выполнить sh это? Как я могу ввести регистрацию простого инжектора в 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);
Мы используем:
- Simple Инжектор 4.7.1
- SimpleInjector.Integration.AspNetCore + MVC 4.0.12