简体   繁体   English

问题解决与Unity的依赖关系

[英]Issue resolving dependencies with Unity

As soon as i'm trying to resolve my unitOfWork i get this error : 一旦我试图解决我的unitOfWork我得到这个错误:

"The type IUnitOfWork does not have an accessible constructor." “IUnitOfWork类型没有可访问的构造函数。”

However this only happens when i set the LifetimeManager of the unitOfWork to PerResolveLifetimeManager. 但是,只有当我将unitOfWork的LifetimeManager设置为PerResolveLifetimeManager时才会发生这种情况。 If I'm just using the default one, everything works fine. 如果我只是使用默认的,一切正常。 My unitOfWork, do have a public parameterless constructor. 我的unitOfWork,有一个公共无参数构造函数。 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: 您可以在调用RegisterInstance方法时指定以下Unity内置生命周期管理器类型之一或您的自定义类型:

  1. ContainerControlledLifetimeManager ContainerControlledLifetimeManager
  2. ExternallyControlledLifetimeManager ExternallyControlledLifetimeManager
  3. HierarchicalLifetimeManager 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. 注意:PerResolveLifetimeManagerTransientLifetimeManagerRegisterInstance一起使用是PerResolveLifetimeManager ,因为它们都会在每次要解析的调用上创建一个新实例。

Taken from the official documentation on Unity 2.0, check the section on Using a Lifetime Manager with the RegisterInstance Method. 从Unity 2.0的官方文档中 ,请查看使用RegisterInstance方法使用Lifetime Manager一节。

RegisterInstance is used when you want to register an existing object with unity Container.Each time there is a request for this type , the same instance of object is returned(instead of new object). 当您想要使用unity Container注册现有对象时使用RegisterInstance。每当有此类型的请求时,返回相同的对象实例(而不是新对象)。 By default RegisterInstance method has ContainerControlledLifetimeManager which manages one instance throught the lifetime of Container. 默认情况下,RegisterInstance方法具有ContainerControlledLifetimeManager,它在Container的生命周期内管理一个实例。

In the case of PerResolveLifetimeManager, Each time a request is made to resolve, a new instance of object is created. 在PerResolveLifetimeManager的情况下,每次请求解析时,都会创建一个新的对象实例。

Thus when you try to use PerResolveLifetimeManager along with RegisterInstance Method.The error is thrown back to you. 因此,当您尝试使用PerResolveLifetimeManager和RegisterInstance方法时。错误将被抛回给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM