registring类型时的统一异常

时间:2015-07-02 23:59:04

标签: c# model-view-controller unity-container

我正在与团结合作,我创建了一个无效的测试。

当我运行webApp并从container.LoadConfiguration()方法调用RegisterTypes()时,我收到InvalidOperationException,其中包含以下消息:

  

无法解析类型名称或别名IHolaMundo。请检查   您的配置文件并验证此类型名称。

这是我的代码:

unity.config flie:

<?xml version="1.0"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">


  <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

  <assembly name="unityTest.Data.Hola" />
    <assembly name="unityTest.AppServices.Interfaces" />
    <assembly name="unityTest.AppServices.Servicios" />
    <assembly name="unityTest.UI.ServiceInterfaces" />

    <namespace name="unityTest.Data.Hola" />
    <namespace name="unityTest.AppServices.Interfaces" />
    <namespace name="unityTest.AppServices.Servicios" />
    <namespace name="unityTest.UI.ServiceInterfaces" />

  <container>
    <register type="IHolaMundo" mapTo="HolaMundo"/>
    <register type="IServicioHola" mapTo="ServicioHola"/>
  </container>
</unity>

HolaMundo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unityTest.AppServices.Interfaces;

namespace unityTest.Data.Hola
{
    public class HolaMundo
        : IHolaMundo
    {
        private string _saludo = "";

        public HolaMundo()
        {
            _saludo = "Hola Mundo";
        }
        public string Saludo()
        {
            return _saludo;

        }
    }
}

IHolaMundo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace unityTest.AppServices.Interfaces
{
    public interface IHolaMundo
    {
        string Saludo();
    }
}

ServicioHola.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unityTest.AppServices.Interfaces;

namespace unityTest.AppServices.Servicios
{
    public class ServicioHola
    {
        IHolaMundo _miSaludador = null;  

        public ServicioHola(IHolaMundo saludador)
        {
            _miSaludador = saludador;
        }

        public string Saludar()
        {
            return _miSaludador.Saludo();
        }
    }
}

...和HomeController构造函数:

private IServicioHola _holaService = null;
        public HomeController(IServicioHola servicio)
        {
            _holaService = servicio;
        }

任何想法?

1 个答案:

答案 0 :(得分:0)

从提供的XML配置中,注册的程序集名称和命名空间名称相同。请帮助仔细检查项目的程序集名称。我假设项目程序集名称为unityTest.DataunityTest.AppServices<assembly name="..."/>,这可能在您的Unity XML配置中缺失。

请确保已在-ex py

中配置了正确的程序集名称

步骤: 1.在解决方案资源管理器中选择项目后,在“项目”菜单上单击“属性”。 2.单击Application选项卡,然后选中Assembly Name字段。