c# – Unity 3和错误“无法解析类型名称或别名”xxxxx“.请检查您的配置文件并验证此类型名称.“

前端之家收集整理的这篇文章主要介绍了c# – Unity 3和错误“无法解析类型名称或别名”xxxxx“.请检查您的配置文件并验证此类型名称.“前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法解决Unity 3的这个问题?

我已经尽一切可能绕过这个消息错误,但我无法解决;我已经做了我在googles搜索中看到的所有内容.

我几乎放弃并尝试另一种DI解决方案.

我的配置文件

<?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:

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();
        }
    }
}

我的Bootstrapper类:

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;
        }
    }
}

我的Dependency项目静态类:

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框架.

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工作原因导致我在映射过程中出现错误,在我的Bootstrapper类,BuildUnityContainer方法下面的行中:

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

错误是:

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

我已经仔细检查了所有课程,对我来说,他们还可以.还是缺少什​​么?

解决方法

问题在于配置文件.您正在使用一些不正确的语法混合两个概念.

< assembly ... />和< namespace ... />当您的< register ... />时,节点提供程序集和命名空间搜索顺序node包含一个自身无法找到的类型.如果找不到类型,它会搜索[namespace] .Type,[assembly]的所有组合.这是错误的位置:它不搜索Type,[assembly].如果有任何< namespace ... />节点已定义,它不会尝试只附加程序集.

所以你的< register type =“Biblioteca.Contracts.IManterCategoriaBO”mapTo =“Biblioteca.Business.ManterCategoriaBO”/>节点的类型为Biblioteca.Contracts.IManterCategoriaBO,它不包含程序集,因此无法找到它.因此,它需要进行搜索.您确实指定了< namespace ... />节点,所以它将首先尝试Biblioteca.Biblioteca.Contracts.IManterCategoriaBO,Biblioteca.请注意重复的Biblioteca名称.

这是一个更正的配置文件.

<?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>
原文链接:https://www.f2er.com/csharp/92267.html

猜你在找的C#相关文章