C# Unity 3 和错误“无法解析类型名称或别名“xxxxx”。请检查您的配置文件并验证此类型名称。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18668240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unity 3 and Error "The type name or alias "xxxxx" could not be resolved. Please check your configuration file and verify this type name."
提问by Olivertech
Is there any way to resolve this problem with Unity 3 ?
有什么办法可以用 Unity 3 解决这个问题?
I have made all that is possible to bypass this message error, but I can't resolve; I have already did everything I've seen in googles searches.
我已尽一切可能绕过此消息错误,但我无法解决;我已经做了我在谷歌搜索中看到的一切。
I am almost giving up and trying another DI solution.
我几乎要放弃并尝试另一种 DI 解决方案。
My configuration 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="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
</container>
</unity>
</configuration>
My interface:
我的界面:
using Biblioteca.Transport;
using System.Linq;
namespace Biblioteca.Contracts
{
public interface IManterCategoriaBO
{
IQueryable<CategoriaDTO> GetAll();
CategoriaDTO GetById(int id);
void Insert(CategoriaDTO dto);
}
}
My concrete class:
我的具体课程:
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();
}
}
}
My Global.asax:
我的 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>();
}
}
}
My UI model class file on MVC 4 project. I am using 4.5 framework.
我在 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);
}
}
}
}
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:
我认为一切都以正确的方式进行。但是,我看不到这个 DI 工作,因为我在映射过程中遇到了一个错误,在我的 Bootstrapper 类的 BuildUnityContainer 方法中:
var container = new UnityContainer().LoadConfiguration(unitySection);
var container = new UnityContainer().LoadConfiguration(unitySection);
The error is:
错误是:
The type name or alias Biblioteca.Contracts.IManterCategoriaBO could not be resolved. Please check your configuration file and verify this type name.
无法解析类型名称或别名 Biblioteca.Contracts.IManterCategoriaBO。请检查您的配置文件并验证此类型名称。
I have double checked all my classes and for me, they are ok. Or is it missing anything ?
我已经仔细检查了我所有的课程,对我来说,他们没问题。或者它缺少什么?
回答by TylerOhlsen
The problem is in you config file. You are mixing two concepts with some incorrect syntax.
问题出在你的配置文件中。您将两个概念与一些不正确的语法混合在一起。
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.
在<assembly... />
和<namespace ... />
当你的节点提供一个集和命名空间搜索顺序<register ... />
节点包含本身不能找到一个类型。如果找不到类型,它会搜索 的所有组合[namespace].Type, [assembly]
。这是错误所在:它不搜索Type, [assembly]
. 如果<namespace ... />
定义了任何节点,它不会尝试仅附加程序集。
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.
因此,您的<register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
节点具有Biblioteca.Contracts.IManterCategoriaBO
不包含程序集的类型,因此无法找到它。因此,它需要进行搜索。您确实指定了<namespace ... />
节点,因此它将首先尝试Biblioteca.Biblioteca.Contracts.IManterCategoriaBO, Biblioteca
. 注意重复的 Biblioteca 名称。
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>
回答by Olivertech
A little information that nobody nowhere says is that I need to make reference to all the projects that will be used by unity.
没有人说的一个小信息是我需要参考所有将被unity使用的项目。
So, in my solution, inside my Biblioteca web project, it was necessary to reference Biblioteca.Business and Biblioteca.Contracts to be able to pass throught the unity register without any error. I was referencing only the last one.
因此,在我的解决方案中,在我的 Biblioteca Web 项目中,有必要引用 Biblioteca.Business 和 Biblioteca.Contracts 才能通过统一寄存器而不会出现任何错误。我只引用了最后一个。
It′s is incredible, but that was my problem !!! I′ve thought the unity was able to make some kind of refletion using all the paths included inside my unity.config file. But I was wrong. To make Unity works, its necessary to reference all the projects that are inside the unity.config file. If I point to some namespace, the related project need to be referenced.
这太不可思议了,但那是我的问题!!!我认为 unity 能够使用我的 unity.config 文件中包含的所有路径进行某种反思。但是我错了。为了使 Unity 工作,有必要引用 unity.config 文件中的所有项目。如果我指向某个命名空间,则需要引用相关项目。
I have already resolved the problem, but I do not agree with this approach to make unity works. But, anyway, its working now !!!
我已经解决了这个问题,但我不同意这种使统一工作的方法。但是,无论如何,它现在可以工作了!!!
Thanks any way Tyler, for your support.
无论如何,感谢泰勒,感谢您的支持。
回答by TylerOhlsen
As stated in the previous comment, you could do the reflection assembly load yourself before calling UnityContainer.LoadConfiguration(...)
. Here's a generic way to do so.
正如之前的评论中所述,您可以在调用UnityContainer.LoadConfiguration(...)
. 这是一个通用的方法。
Just be sure your assemblies can be found by name. This can be done by placing the assembly in your application's bin directory. Or add a <probing>
path to your app config. Or add them to the Global Assembly Cache (GAC), but I don't recommend this.
只要确保您的程序集可以通过名称找到。这可以通过将程序集放在应用程序的 bin 目录中来完成。或者<probing>
向您的应用程序配置添加路径。或者将它们添加到全局程序集缓存 (GAC),但我不建议这样做。
private static void LoadDependencyAssemblies()
{
UnityConfigurationSection section = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
if (section == null)
{
throw new ConfigurationErrorsException("Unable to locate Unity configuration section.");
}
foreach (string assemblyName in section.Assemblies.Select(element => element.Name))
{
try
{
Assembly.Load(assemblyName);
}
catch (Exception ex)
{
throw new ConfigurationErrorsException("Unable to load required assembly specified in Unity configuration section. Assembly: " + assembly, ex);
}
}
}
回答by MvdD
I just spend a good portion of my day troubleshooting this error.
我只是花了一天的大部分时间来解决这个错误。
In my case there was no error in the configuration, but it turned out that one of the assemblies holding the concrete types was depending on another assembly that it could not find.
在我的例子中,配置没有错误,但结果表明,其中一个包含具体类型的程序集依赖于它无法找到的另一个程序集。
Unfortunately, Unity did not hint at any issues loading the assembly. In fact, the assembly showed up in the Modules window in the Visual Studio Debugger. Still the type could not be loaded.
不幸的是,Unity 没有提示加载程序集的任何问题。事实上,程序集显示在 Visual Studio 调试器的模块窗口中。仍然无法加载类型。
回答by Chad Hedgcock
If you're here from google, the simplest way to get it working is to remove the <alias>
and <namespace>
stuff and do the following.
如果您来自谷歌,最简单的方法是删除<alias>
和<namespace>
内容并执行以下操作。
<container>
<register type="MyApp.Repositories.IUserRepository, MyApp" mapTo="MyApp.Repositories.UserRepository, MyApp" />
</container>
...where MyApp
is your Assembly Name from project Properties -> Application.
...MyApp
从项目属性-> 应用程序中您的程序集名称在哪里。
回答by rusty
I was receiving this error message as well but found another set of causes :
我也收到了此错误消息,但发现了另一组原因:
- I am using a WiX installer which was not properly deploying some of the dlls required for the target assembly (In this example it would be dependencies of the Biblioteca.Business). So double check that.
- I was then getting the error message only when running in release mode, debug mode worked fine. Turns out the target mapTo class was not marked with "public" attribute. For some reason this did not matter in debug mode but is a blocker in release mode.
- 我使用的 WiX 安装程序没有正确部署目标程序集所需的一些 dll(在本例中,它将是 Biblioteca.Business 的依赖项)。所以仔细检查一下。
- 然后我只在发布模式下运行时才收到错误消息,调试模式工作正常。原来目标 mapTo 类没有用“public”属性标记。出于某种原因,这在调试模式下无关紧要,但在发布模式下会阻止。
Hope that helps someone since it blocked me for two days.
希望对某人有所帮助,因为它阻止了我两天。