JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
563 stars 118 forks source link

Error while attempting to register instance as Singleton #281

Closed sid5 closed 2 years ago

sid5 commented 3 years ago

I'm in the process of attempting to migrate our solution from StructureMap to Lamar, and I'm encountering the following error any time I attempt to register a particular dependency as a singleton.

'System.Object' cannot be used for constructor parameter of type 'IMyInterface'

(This error occurs when calling Container.AssertConfigurationIsValid().)

Unfortunately, I can't share much of the code, but this is the relevant bit:

For<IMyInterface>()
    .Use(getImplementation()) // getImplementation() returns an IMyInterface
    .Singleton();

Curiously, if I register this as anything other than a singleton, it appears to work as expected.

While investigating this, I found issue #159 , which appears to be the same issue I'm encountering, although that was marked as closed and fixed as of v 3.0.4. (I'm using v 5.0.3.) I've tried the workaround mentioned in that issue to no avail.

Is there something that I'm doing wrong here? If not, is there a workaround that would allow me to register an instance as a singleton?

jeremydmiller commented 3 years ago

@sid5 Sorry for the delay answering this. First off, can you just cast it like so:

For<IMyInterface>()
    .Use((IMyInterface)getImplementation()) 
    .Singleton();

or even:

For<IMyInterface>()
    .Use(s => (IMyInterface)getImplementation()) 
    .Singleton();
jeremydmiller commented 2 years ago

No recent activity, and this wasn't a bug to begin with.