0
votes

I've met an exception while resolving the object using Unity container:

Message "Value cannot be null.\r\nParameter name: container" Source "Microsoft.Practices.Unity" string

I have a Prism application where I have ServiceModule. ServiceModule just has interface and its implementation:

public interface ICommonService
{
    string SomeStorage { get; set; }        
}

public class CommonService : ICommonService
{
    string fooStorage=DateTime.Now.ToString();
    public string FooStorage
    {
        get
        {
           return fooStorage;
        }
        set
        {
            fooStorage = value;
            OnPropertyChanged("FooStorage");                                
        }
    }
}

I create a single instance of ICommonService in ViewModelA of ModuleA. It works okay:

unityContainer = new UnityContainer();
unityContainer.RegisterType<ICommonService, CommonService>(new ContainerControlledLifetimeManager());
IMyService someFoo = unityContainer.Resolve<ICommonService>();
someFoo.FooStorage = "AAAOOOBBB";

Then I want to read this data in viewModelB of ModuleB. And this code throws an exception:

ICommonService someFoo = unityContainer.Resolve<ICommonService>();
string str=someFoo.FooStorage;

The exception says:

Message "Value cannot be null.\r\nParameter name: container" Source "Microsoft.Practices.Unity" string

What am I doing wrong? Any help will be greatly appreciated!

1
What are IMyService and MyService? - David Arno
Is it ICommonyService or IMyService? - Yacoub Massad
In viewModelB, how do you get an instance of the container? is the value of unityContainer null? Can you debug to verify? - Yacoub Massad

1 Answers

2
votes

Based on the error message that you get, most probably the value of unityContainer is null. Make sure that you initialize it correctly.