We use Simple Injector for our application components and we need to access a component inside of an IdentityServer interface implementation (IResourceOwnerPasswordValidator), which itself is inside IServiceCollection. If I'm understanding it correctly we need to implement ASP.NET Core MVC integration 4.0-4.5 and use container.CrossWire<IResourceOwnerPasswordValidator>(app) (instead of AutoCrossWireAspNetComponents() since we target net452 in the ASP.NET Core 1.1 project). When I try to inject ISignInManager into ResourceOwnerPasswordValidator constructor after trying this implementation I get this error during verification:
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 'ISignInManager' while attempting to activate 'ResourceOwnerPasswordValidator'.
Full exception:
{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()}
How can I accomplish this? How can I inject a Simple Injector registration into ASP.NET DI registration?
Registrations:
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);
We are using:
- Simple Injector 4.7.1
- SimpleInjector.Integration.AspNetCore+MVC 4.0.12
The exception states:
This means that
ResourceOwnerPasswordValidatordepends onISignInManagerbut noISignInManageris registered in the ASP.NET Core configuration system.