3

I'm trying to "resolve" a class using Unity Dependency Injection, but Unity always returns me System.NullReferenceException

private IUnityContainer _unityContainer { get; set; }
public void myMethod()
{
    _unityContainer = UnityContainer.UnityContainerConfig.Initialize();
    IMyclass ObjMyClass = _unityContainer.Resolve<IMyclass >();
    ObjMyClass .Init();
}

My container:

public class UnityContainerConfig
{
    public static IUnityContainer _unityContainer = null;

    public static IUnityContainer Initialize()
    {
        if (_unityContainer == null)
        {
            _unityContainer = new Microsoft.Practices.Unity.UnityContainer()
            .RegisterType<IRepository<Table1>, Table1Repository>(new HttpContextLifetimeManager<IRepository<Table1>>())
            .RegisterType<IRepository<Table2>, Table2Repository>(new HttpContextLifetimeManager<IRepository<Table2>>())
            .RegisterType<IRepository<Table3>, Table3Repository>(new HttpContextLifetimeManager<IRepository<Table3>>())
            .RegisterType<IRepository<Table4>, Table4Repository>(new HttpContextLifetimeManager<IRepository<Table4>>())
            .RegisterType<IMyclass, MyClass>(new HttpContextLifetimeManager<IMyclass>());
        }
    }
        return unityContainer;
}

There are 4 parameters in "my class" constructor which receives the "4 tables as repository"

2
  • Which line throws the exception? Commented Aug 23, 2016 at 20:16
  • IMyclass ObjMyClass = _unityContainer.Resolve<IMyclass>(); Commented Aug 24, 2016 at 19:14

1 Answer 1

1

The problem have been solved.

I had to re-install the unity (nuget), install UnityOfWork and register the "UnityOfWork class"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.