问题解决依赖使用Unity [英] Issue resolving dependencies with Unity

查看:1102
本文介绍了问题解决依赖使用Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图解决我的UnitOfWork我得到这个错误:



的类型IUnitOfWork不具有可访问的构造函数



但是当我的UnitOfWork的LifetimeManager设置PerResolveLifetimeManager这只是发生。如果我只是使用的是默认的,一切工作正常。
我的UnitOfWork,确实有一个公共无参数构造函数。
这是我的代码:

  //全球ASAX 
IUnityContainer团结=新UnityContainer();
unity.RegisterType<&HomeController的GT;();
unity.RegisterInstance&所述; IUnitOfWork>(新的UnitOfWork(),新PerResolveLifetimeManager());
ControllerBuilder.Current.SetControllerFactory(新IocControllerFactory(单位));

// IocControllerFactory
公共类IocControllerFactory:DefaultControllerFactory
{
私人只读IUnityContainer _container;

公共IocControllerFactory(IUnityContainer容器)
{
_container =容器;
}

保护覆盖一个IController GetControllerInstance(RequestContext的RequestContext的,类型controllerType)
{
如果(controllerType!= NULL)
返回_container.Resolve(controllerType )作为一个IController;
,否则
返回base.GetControllerInstance(RequestContext的,controllerType);
}
}

//首页的控制器构造
公众的HomeController(IUnitOfWork的UnitOfWork)
{
}


解决方案

您可以指定以下统一的一个内置终身经理
类型或当你调用 RegisterInstance 方法您的自定义类型:




  1. ContainerControlledLifetimeManager

  2. ExternallyControlledLifetimeManager

  3. HierarchicalLifetimeManager



注意:这是不恰当的,以使用 PerResolveLifetimeManager TransientLifetimeManager
RegisterInstance 因为它们都建立在每次调用
的新实例来解决。




这href=\"http://msdn.microsoft.com/en-us/library/ff660894%28v=pandp.20%29.aspx\" rel=\"nofollow\">官方文档的团结2.0,检查节上使用的方法RegisterInstance终生经理。


As soon as i'm trying to resolve my unitOfWork i get this error :

"The type IUnitOfWork does not have an accessible constructor."

However this only happens when i set the LifetimeManager of the unitOfWork to PerResolveLifetimeManager. If I'm just using the default one, everything works fine. My unitOfWork, do have a public parameterless constructor. This is my code :

//Global asax
IUnityContainer unity = new UnityContainer();
unity.RegisterType<HomeController>();
unity.RegisterInstance<IUnitOfWork>(new UnitOfWork(), new PerResolveLifetimeManager()); 
ControllerBuilder.Current.SetControllerFactory(new IocControllerFactory(unity));

//IocControllerFactory 
public class IocControllerFactory : DefaultControllerFactory
{
    private readonly IUnityContainer _container;

    public IocControllerFactory(IUnityContainer container)
    {
        _container = container;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType != null)
            return _container.Resolve(controllerType) as IController;
        else
            return base.GetControllerInstance(requestContext, controllerType);
    }
}

//Home controller constructor
public HomeController(IUnitOfWork unitOfWork)
{
}

解决方案

You can specify one of the following Unity Built-In Lifetime Managers types or your custom type when you call the RegisterInstance method:

  1. ContainerControlledLifetimeManager
  2. ExternallyControlledLifetimeManager
  3. HierarchicalLifetimeManager

Note: It is not appropriate to use either PerResolveLifetimeManager or TransientLifetimeManager with RegisterInstance since they both create a new instance on every call to resolve.

Taken from the official documentation on Unity 2.0, check the section on Using a Lifetime Manager with the RegisterInstance Method.

这篇关于问题解决依赖使用Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆