8
votes

I've written a class that has some dependencies it resolves from the unity container.

From my main class I create a new object

MyObject myObject = new MyObject();

I register it with my Unity Container

UContainer.RegisterInstance<MyObject>(myObject, new ExternallyControlledLifetimeManager());

the I create the type that needs this as a dependency

ConsumerObject consumer = new ConsumerObject();

the consumer looks like this:

public class ConsumerObject
{
    public ConsumberObject()
    {
         theObject = (MyObject)UContainer.Resolve(typeof(MyObject));    
    }
}

this throws an exception:

Resolution of the dependency failed, type = "MyObject", name = "". Exception message is: The current build operation (build key Build Key[MyObject, null]) failed: The parameter pp could not be resolved when attempting to call constructor MyObject(IPreferenceStorageProvider pp). (Strategy type BuildPlanStrategy, index 3)

Why is my resolve call trying to call another contsructor on the type? I already created it and registered the instance.. I also tried it like: theObject = UContainer.Resolve<MyObject>(); doesn't seem to make any difference..

Thanks

4

4 Answers

14
votes

I think the problem is that you using ExternallyControlledLifetimeManager. In this case Unity container holds only weak reference to your instance. And when you try to resolve, your instance already garbage collected. That's why the default LifeTimeManager for .RegisterInstance() is ContainerControlledLifeTimeManager. And Darrel Miller's case it works, because it not GC-ed yet. Try register your instance this way:

UContainer.RegisterInstance<MyObject>(myObject);
3
votes

I'm not sure why you are seeing the behaviour you are. I just created a test that duplicated your scenario and it worked fine.

Have you tried something like this,

public class ConsumerObject
{
    public ConsumberObject(MyObject myObject)
    {
         theObject = myObject
    }
}

and then using UContainer.Resolve<MyObject>() ?

The only thing that I can think of is when you access UContainer.RegisterInstance and then UContainer.Resolve you are actually accessing two different containers. Could you show us how you are declaring UContainer?

1
votes

As far as I know Unity tries (by default) to call the constructor with largest number of parameters and tries to resolve each of the parameters from the mappings. You shold add the mapping for IPreferenceStorageProvider or remove the constructor that requires this parameter.

If you don't want the IPreferenceStorageProvider parameter to by injected by unity maybe it shouldn't be declared as a constructor parameter at all. You can hard code instantiation of this object in default constructor.

0
votes

"MyObject" is a type that implements an interface i presume. Try registering with the interface instead of the actual implementation.

something like:

UContainer.RegisterInstance<IMyObject>(myObject, new ExternallyControlledLifetimeManager());

Also if you have multiple implementation of the same interfeace, don't forget to name them upon registration, unless you want to override the already registered implementation.

For further help, i suggest you visit msdn:

https://msdn.microsoft.com/en-us/library/ff648449.aspx