Mismatched and Ambiguous lifestyles - DI in .Net with SimpleInjector
I have added a new service to exiting solution. It was diligently registered in entry (startup) project like:
container.Register(Lifestyles.Scoped);
I injected it in an Web API controller, pressed F5 and boo… got this exception:
SimpleInjector.DiagnosticVerificationException occurred HResult=0x80131500 Message=The configuration is invalid. The following diagnostic warnings were reported: -[Short Circuited Dependency] FabulousApiController might incorrectly depend on unregistered type FabulousConcept (Transient) instead of IFabulousConcept (Web Request). -[Ambiguous Lifestyles] The registration for FabulousConcept (Transient) maps to the same implementation (FabulousConcept) as the registration for IFabulousConcept (Web Request) does, but the registration maps to a different lifestyle. This will cause each registration to resolve to a different instance. -[Ambiguous Lifestyles] The registration for IFabulousConcept (Web Request) maps to the same implementation (FabulousConcept) as the registration for FabulousConcept (Transient) does, but the registration maps to a different lifestyle. This will cause each registration to resolve to a different instance. See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings. Source=SimpleInjector StackTrace: at SimpleInjector.Container.ThrowOnDiagnosticWarnings() at SimpleInjector.Container.Verify(VerificationOption option) at Fabulous.Website.Startup.ConfigureDependencyResolution() in C:\Source\Fabulous.Website\App_Start\Startup.DI.cs:line 31 at Fabulous.Website.Startup.Configuration(IAppBuilder app) in C:\Source\Fabulous.Website\Startup.cs:line 24
I was baffeled, as I think I know a bit about SimpleInjector. After beating around the bush for sometime, I hit it.
It was rather innocent (really?) mistake of declaring:
private readonly FabulousConcept _fc;
and then using Resharper to take care of initing it from constructor of the function. There it was! Instead of interface IFabulousConcept
, which was registered with SimpleInjector, I was asking for FabulousConcept class which was unregistered and thus treated as a transient lifestyle.
Changing above declaration (and initializing code in constructor) to following fixed it:
private readonly IFabulousConcept _fc;
Happy Coding!

















