Ninject exception: Several CachingService constructors have the same priority
LazyCache version: 2.0.0 LazyCache.Ninject version: 2.0.0 Ninject version: 3.3.4
I have followed the example in the wiki by passing the LazyCacheModule into the StandardKernel constructor, however I get the following error upon trying to get an IAppCache from the Kernel:
Error activating IAppCache using binding from IAppCache to CachingService Several constructors have the same priority. Please specify the constructor using ToConstructor syntax or add an Inject attribute.
Constructors: CachingService(Func{ICacheProvider} cacheProviderFactory) CachingService(ICacheProvider cache)
Activation path:
- Request for IAppCache
Suggestions:
- Ensure that the implementation type has a public constructor.
- If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
I had the same issue - ended up adding this section to my own NinjectModule that I already passed into my StandardKernel on console app startup.
// LazyCache
Bind<IOptions<MemoryCacheOptions>>().ToConstant( Options.Create( new MemoryCacheOptions() ) );
Bind<IMemoryCache>().To<MemoryCache>().InSingletonScope();
Bind<ICacheProvider>().To<MemoryCacheProvider>().InSingletonScope();
Bind<IAppCache>().ToConstructor(
ctorArg => new CachingService( ctorArg.Inject<ICacheProvider>() ) );
Not sure what I was doing wrong with LazyCacheModule, but I'd prefer to use it.
I also went for a workaround, I just used:
_kernel.Bind<IAppCache>().ToConstructor(x=> new CachingService());
But I would much prefer to use the LazyCacheModule.
Anyone?
Is anyone able to spot what I am doing wrong in the unit tests that cover this functionality as they seem to be passing despite this bug report?
https://github.com/alastairtree/LazyCache/blob/master/LazyCache.Ninject.UnitTests/LazyCacheModuleTest.cs
Are you also using microsoft extensions dependency injection? If so I think this should be fixed in 2.0.4 as i have made the constructor selection logic explicit. Can you give it a test and let me know? Thanks
Closing as probably fixed, file a new issue asking to reopen this if required. Thanks.
No this is not resolved in 2.0.4, the same error occurs.
Thanks for getting back to me, have reopened this. Can you give a bit more information to help me reproduce this please? Ideally a failing unit test like the existing one covering the ninject integration would really help.
Ok after much testing I have found the issue.
The error arises when Ninject.Extensions.Factory (3.3.2) is installed into the project.
It looks like the presence of this package causes Ninject to consider more constructors, which then leads to the exception above.
The reason I have this package installed is because I use Ninject.Extensions.Conventions, which uses it as a dependency.
Any update?
Thanks @TimSirmovics for your research. I too have this issue and am using Ninject.Extensions.Factory as well.
Using the work around for now.