Unity Dependency Injection解析返回System.NullReferenceException

时间:2016-08-23 18:09:08

标签: c# asp.net dependency-injection dependencies unity-container

我正在尝试使用Unity Dependency Injection“解析”一个类,但Unity总是返回我System.NullReferenceException

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

我的容器:

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;
}

“my class”构造函数中有4个参数接收“4个表作为存储库”

1 个答案:

答案 0 :(得分:0)

问题已经解决了。

我不得不重新安装Unity(nuget),安装UnityOfWork并注册“UnityOfWork类”