统一3和错误"类型名称或别名"为XXXXX"无法解析。请检查您的配置文件并验证这种类型的名称和QUOT。 [英] Unity 3 and Error "The type name or alias "xxxxx" could not be resolved. Please check your configuration file and verify this type name."

查看:1712
本文介绍了统一3和错误"类型名称或别名"为XXXXX"无法解析。请检查您的配置文件并验证这种类型的名称和QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来解决这个问题,统一3 ???

Is there any way to resolve this problem with Unity 3 ???

我做了一切可能绕过此消息错误,但我不能化解......我AM 2天尝试解决,并已经做了一切我已经看到了谷歌搜索。

I have made all that is possible to bypass this message error, but I cant resolve... I am 2 days trying to resolve, and have already did everything I've seen in googles searches.

我几乎放弃并尝试其他DI解决方案。请,任何帮助将被罚款......

I am almost giving up and trying another DI solution. Please, any help will be fine...

我的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Biblioteca" />
    <assembly name="Biblioteca.Contracts" />
    <assembly name="Biblioteca.Business" />
    <namespace name="Biblioteca" />
    <namespace name="Biblioteca.Contracts" />
    <namespace name="Biblioteca.Business" />
      <container>
        <register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
      </container>
  </unity>
</configuration>

我的界面:

using Biblioteca.Transport;
using System.Linq;

namespace Biblioteca.Contracts
{
    public interface IManterCategoriaBO
    {
        IQueryable<CategoriaDTO> GetAll();
        CategoriaDTO GetById(int id);
        void Insert(CategoriaDTO dto);
    }
}

我的具体类:

using Biblioteca.Contracts;
using Biblioteca.Transport;
using Biblioteca.Data;
using System;
using System.Linq;

namespace Biblioteca.Business
{
    public class ManterCategoriaBO : IManterCategoriaBO
    {
        public CategoriaDTO GetById(int id)
        {
            CategoriaDTO dto = new CategoriaDTO();
            ManterCategoriaDO categoriaDO = new ManterCategoriaDO();

            dto = categoriaDO.GetById(1);

            return dto;
        }

        public IQueryable<CategoriaDTO> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Insert(CategoriaDTO dto)
        {
            throw new NotImplementedException();
        }
    }
}

我的Global.asax:

My Global.asax:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Biblioteca.Dependency;

namespace Biblioteca
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //Below is a static variable to take the unity container
            //which is on a dependency project
            Global.Container = Bootstrapper.Initialise();
        }
    }
}

我的引导程序类:

My Bootstrapper class:

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.Web.Mvc;
using Unity.Mvc4;

namespace Biblioteca
{
    public static class Bootstrapper
    {
        public static IUnityContainer Initialise()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            return container;
        }

        private static IUnityContainer BuildUnityContainer()
        {
            string path = ConfigurationManager.AppSettings["UnityConfigFilePath"].ToString();

            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = path + "\\Unity.config" };

            System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

            //*** this line is firing the error !!! ****
            var container = new UnityContainer().LoadConfiguration(unitySection);

            return container;
        }
    }
}

我的依赖项目静态类:

My Dependency project static class:

using Microsoft.Practices.Unity;

namespace Biblioteca.Dependency
{
    public static class Global
    {
        public static IUnityContainer Container = null;

        public static T Resolve<T>()
        {
            return Container.Resolve<T>();
        }
    }
}

这是MVC 4项目我的UI模型类文件。我使用的4.5框架。

My UI model class file on MVC 4 project. I am using 4.5 framework.

using Biblioteca.Contracts;
using Biblioteca.Dependency;

namespace Biblioteca.Models
{
    public class LivroModel
    {
        public void GetAll()
        {
            if (Global.Container != null)
            {
                var categoriaBO = Global.Resolve<IManterCategoriaBO>();
                categoriaBO.GetById(1);
            }
        }
    }
}

我觉得一切都在正确的道路。但是,我不能看到这个作品的DI,因为我只是在映射过程得到了一个错误,在下面一行在我的引导程序类,BuildUnityContainer方式:

I think everything is in the right way. But, I can´t see this DI works cause I got an error just in the mapping process, in line below on my Bootstrapper class, BuildUnityContainer method:

VAR集装箱=​​新UnityContainer()LoadConfiguration(unitySection);

var container = new UnityContainer().LoadConfiguration(unitySection);

该错误是:

键入名称或别名Biblioteca.Contracts.IManterCategoriaBO可能
  无法得到解决。请检查您的配置文件并验证该
  类型名称。

The type name or alias Biblioteca.Contracts.IManterCategoriaBO could not be resolved. Please check your configuration file and verify this type name.

我有双重检查我的所有类和我来说,他们是ok了。抑或是缺少什么?

I have double checked all my classes and for me, they are ok. Or is it missing anything ?

我需要帮助,因为我有一个很短的时间来照顾这....

I need help, cause I have a short time to take care of this....

问候,
马塞洛。

Regards, Marcelo.

推荐答案

这个问题在你的配置文件。你是混合两个概念的一些不正确的语法。

The problem is in you config file. You are mixing two concepts with some incorrect syntax.

&LT;装配... /&GT; &LT;命名空间... /&GT; 节点提供了一个集和命名空间的搜索顺序当你的&LT;注册... /&GT; 节点包含自身不能找到一个类型。如果无法找到一个类型,它通过搜索的所有组合[命名空间]。键入[装配] 。这里就是错误的是:它不会搜索键入[装配] 。如果任何&LT;命名空间... /方式&gt; 中定义的节点,它不会尝试只追加装配

The <assembly... /> and <namespace ... /> nodes provide an assembly and namespace search order when your <register ... /> node contains a type that cannot be found by itself. If a type cannot be found, it searches through all combinations of [namespace].Type, [assembly]. Here's where the error is: it does NOT search for Type, [assembly]. If any <namespace ... /> nodes are defined, it does NOT try appending only the assembly.

所以你的&LT;注册类型=Biblioteca.Contracts.IManterCategoriaBOmapTo =Biblioteca.Business.ManterCategoriaBO/&GT; 节点类型 Biblioteca.Contracts.IManterCategoriaBO 不含有该组件,因此它不能被发现。因此,它需要做一个搜索。你没有指定&LT;命名空间... /&GT; 节点,所以它会首先尝试 Biblioteca.Biblioteca.Contracts.IManterCategoriaBO,藏书。注意重复的藏书名。

So your <register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" /> node has the type Biblioteca.Contracts.IManterCategoriaBO which does not contain the assembly, so it cannot be found. Therefore, it needs to do a search. You did specify <namespace ... /> nodes, so it will first try Biblioteca.Biblioteca.Contracts.IManterCategoriaBO, Biblioteca. Notice the duplicate Biblioteca name.

下面是一个更正配置文件。

Here's a corrected config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Biblioteca" />
    <assembly name="Biblioteca.Contracts" />
    <assembly name="Biblioteca.Business" />
    <namespace name="Biblioteca" />
    <namespace name="Biblioteca.Contracts" />
    <namespace name="Biblioteca.Business" />
    <container>
      <register type="IManterCategoriaBO" mapTo="ManterCategoriaBO" />
      <!-- Or this works -->
      <!--<register type="Biblioteca.Contracts.IManterCategoriaBO, Biblioteca" mapTo="Biblioteca.Business.ManterCategoriaBO, Biblioteca" />-->
    </container>
  </unity>
</configuration>

这篇关于统一3和错误&QUOT;类型名称或别名&QUOT;为XXXXX&QUOT;无法解析。请检查您的配置文件并验证这种类型的名称和QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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