LazyCache icon indicating copy to clipboard operation
LazyCache copied to clipboard

Ninject exception: Several CachingService constructors have the same priority

Open TimSirmovics opened this issue 6 years ago • 11 comments

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:

  1. Request for IAppCache

Suggestions:

  1. Ensure that the implementation type has a public constructor.
  2. If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

TimSirmovics avatar Jun 11 '19 05:06 TimSirmovics

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.

andrewalkema avatar Jun 18 '19 14:06 andrewalkema

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.

TimSirmovics avatar Jun 18 '19 21:06 TimSirmovics

Anyone?

TimSirmovics avatar Jun 27 '19 13:06 TimSirmovics

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

alastairtree avatar Dec 28 '19 02:12 alastairtree

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

alastairtree avatar Dec 28 '19 14:12 alastairtree

Closing as probably fixed, file a new issue asking to reopen this if required. Thanks.

alastairtree avatar Jan 04 '20 14:01 alastairtree

No this is not resolved in 2.0.4, the same error occurs.

TimSirmovics avatar Jan 20 '20 03:01 TimSirmovics

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.

alastairtree avatar Jan 20 '20 12:01 alastairtree

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.

TimSirmovics avatar Jan 22 '20 21:01 TimSirmovics

Any update?

TimSirmovics avatar Mar 15 '20 00:03 TimSirmovics

Thanks @TimSirmovics for your research. I too have this issue and am using Ninject.Extensions.Factory as well.

Using the work around for now.

OlympicLarry avatar Oct 19 '20 22:10 OlympicLarry