确保控制器具有一个团结参数的公共构造函数 [英] Make sure that the controller has a parameterless public constructor in Unity

查看:465
本文介绍了确保控制器具有一个团结参数的公共构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个问题的控制器:

发生错误试图创建类型的控制器时,'的 * 的.WebMvc.Controllers.HomeController。确保控制器具有一个无参数公共构造函数。

I got this problem with the Controller:
An error occurred when trying to create a controller of type '*.WebMvc.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.

查找的ApiController的解决方案,但并没有找到有关正常控制器东西。

Find the solution for the ApiController, but didn't find anything about normal Controller.

从头开始创建新的MVC 4项目。

Created new MVC 4 project from scratch.

HomeController.cs:

HomeController.cs:

public class HomeController : Controller
{
    private IAccountingUow _uow;
    public HomeController(IAccountingUow uow)
    {
        _uow = uow;
    }

UnityDependencyResoler.cs:

UnityDependencyResoler.cs:

public class UnityDependencyResolver : IDependencyResolver
{
    private IUnityContainer _container;
    public UnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
        RegisterTypes();
    }
    public object GetService(Type serviceType)
    {
        try
        {
            return _container.Resolve(serviceType);
        }catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _container.ResolveAll(serviceType);
        }catch
        {
            return null;
        }
    }

    private void RegisterTypes()
    {
        _container.RegisterType<IAccountingUow, AccountingUow>();

    }
}

Global.asax中

Global.asax

    protected void Application_Start()
    {
        //Omitted
        DependencyResolver.SetResolver( new UnityDependencyResolver( new UnityContainer()));

    }

调试并发现,有甚至没有尝试解决IAccountingUow。

Debugged and found out, that there are even no attempts to resolve IAccountingUow.

我在做什么错了?
它整天思考。

What i'm doing wrong? Thinking about it whole day.

推荐答案

在哪里的问题。也许有些人会面临同样的。
问题是,统一解决不了 IAccountingUow ,因为接口层次扶养的。

Found where is the issue. Maybe some one will face the same. The problem is that Unity could not resolve the IAccountingUow, because of hierarchical dependancy on interfaces.

AccountingUow 类有两个contuctors

AccountingUow class has two contuctors

    public AccountingUow( IRepositoryProvider repositoryProvider)
    {
        Init(repositoryProvider);
    }
    public AccountingUow()
    {
        Init( new RepositoryProvider(new RepositoryFactories()) );
    }

依赖解析器不聪明足够多采取默认parametless构造器。它试图把界面相关的构造器并没有解决这个问题,导致没有任何规则解决它。

Dependency Resolver is not smart enought to take the default parametless contructor. It tries to take interface dependant contructor and fails to resolve it, cause there are no rules for resolving it.

我注释掉接口依赖构​​造和一切工作正常。

I commented out interface dependant constructor and everything worked fine.

我将稍后发布解析器为第一个构造器,也许有人会使用它。

I will post later resolver for the first contructor, maybe someone will use it.

这篇关于确保控制器具有一个团结参数的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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