Unity 应用程序块 2.0 - 给定的程序集名称或代码库无效
接口(在名为“Interfaces”的程序集中。在项目中:- 接口)
namespace Interfaces
{
public interface IDoSomeWork1
{
string DoSomeWork1();
}
}
namespace Interfaces
{
public interface IDoSomeWork2
{
string DoSomeWork2();
}
}
依赖项(在名为“Entities”的程序集中。在项目中:- 实体)
namespace Entities
{
public class ClassB : IDoSomeWork1
{
public string DoSomeWork1()
{
return this.ToString();
}
}
}
namespace Entities
{
public class ClassC : IDoSomeWork2
{
public string DoSomeWork2()
{
return this.ToString();
}
}
}
类(在项目中:-UsingUnity)
public class ClassA
{
[Dependency]
public IDoSomeWork1 DoSomeWork1 { get; set; }
[Dependency]
public IDoSomeWork2 DoSomeWork2 { get; set; }
public void SomeMethodInClassA()
{
Console.WriteLine(DoSomeWork1.DoSomeWork1());
Console.WriteLine(DoSomeWork2.DoSomeWork2());
}
}
App.Config(在控制台应用程序项目中:-ConsoleUsingUnity)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<containers>
<container>
<types>
<type type="Interfaces.IDoSomeWork1, Interfaces"
mapTo="Entities.ClassB, Entities" />
<type type="Interfaces.IDoSomeWork2, Interfaces"
mapTo="Entities.ClassC, Entities" />
</types>
</container>
</containers>
</unity>
</configuration>
客户端(在控制台应用程序项目中:-ConsoleUsingUnity)
public class Class1
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
// Load from config file
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);
ClassA classA = container.Resolve<ClassA>();
classA.SomeMethodInClassA();
}
}
当我运行客户端时,我在section.Configure(container);:- 处收到以下错误
给定的程序集名称或代码库 无效。 (HRESULT 的异常: 0x80131047)
我不确定配置或类型是否有问题。有人可以指出这里的错误吗?
Interfaces (In the assembly named "Interfaces". In project :- Interfaces)
namespace Interfaces
{
public interface IDoSomeWork1
{
string DoSomeWork1();
}
}
namespace Interfaces
{
public interface IDoSomeWork2
{
string DoSomeWork2();
}
}
Dependencies (In the assembly named "Entities". In project :- Entities)
namespace Entities
{
public class ClassB : IDoSomeWork1
{
public string DoSomeWork1()
{
return this.ToString();
}
}
}
namespace Entities
{
public class ClassC : IDoSomeWork2
{
public string DoSomeWork2()
{
return this.ToString();
}
}
}
Class (In project :- UsingUnity)
public class ClassA
{
[Dependency]
public IDoSomeWork1 DoSomeWork1 { get; set; }
[Dependency]
public IDoSomeWork2 DoSomeWork2 { get; set; }
public void SomeMethodInClassA()
{
Console.WriteLine(DoSomeWork1.DoSomeWork1());
Console.WriteLine(DoSomeWork2.DoSomeWork2());
}
}
App.Config (In a console application project :- ConsoleUsingUnity)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<containers>
<container>
<types>
<type type="Interfaces.IDoSomeWork1, Interfaces"
mapTo="Entities.ClassB, Entities" />
<type type="Interfaces.IDoSomeWork2, Interfaces"
mapTo="Entities.ClassC, Entities" />
</types>
</container>
</containers>
</unity>
</configuration>
The client (In a console application project :- ConsoleUsingUnity)
public class Class1
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
// Load from config file
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);
ClassA classA = container.Resolve<ClassA>();
classA.SomeMethodInClassA();
}
}
And when I run the client, I get the following error at section.Configure(container);:-
The given assembly name or codebase
was invalid. (Exception from HRESULT:
0x80131047)
I am not sure If there is anything wrong with the config or the type. Could anyone please point out the mistake here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
现在有更好的办法了!
Unity 有一个新版本(当前为 2.1.505.2),它清楚地报告了详细信息,让您立即了解情况。
您可以在此处找到二进制文件和源代码:http://unity.codeplex.com/releases
There is a better way now!
Unity has a new version (currently 2.1.505.2) which clearly reports the details and lets you get to the bottom of it immediately.
You can find binaries and source here: http://unity.codeplex.com/releases
我发现查找确切绑定失败的
Type
的最耗时方法如下:DEBUG
版本Unity
和Unity.Configuration
(如果您的项目使用更多的 Unity 程序集,也构建它们)调试>;异常
并确保公共语言运行时异常
在抛出
列中有一个复选框。现在去撞坏那个东西。执行将在 Unity 的
TypeResolverImpl.SearchAssemblies
方法中停止,并且typeNameOrAlias
参数将保存答案!I found that the least time consuming method of finding which
Type
exactly failed to bind is the following:DEBUG
version ofUnity
andUnity.Configuration
(if your project uses more of unity assemblies, build them as well)Debug > Exceptions
and make sureCommon Language Runtime Exceptions
has a checkbox in theThrown
column.Now go crash that thing. Execution will stop in Unitys'
TypeResolverImpl.SearchAssemblies
method andtypeNameOrAlias
parameter will hold the answer!像这样工作
works like this
请确保您已在 web.config 文件所在的项目中添加了缺少程序集的程序集引用。
我错过了这个。我已经在使用 Unity 解析类的项目中添加了这些程序集引用,但错过了将其添加到配置文件所在的父项目中。这解决了我的问题。
Please make sure, that you have added assembly references of the missing assembly in the project where your web.config file exists.
I was missing this. I already have added those assembly references in the project which was using Unity to resolve the class, but missed to add it in the parent project where configuration file was located. This has resolved my problem.
如果其他人遇到同样的问题 - 我也遇到了这个错误,但问题略有不同。我试图加载一个明显存在的程序集,如下所示:
经过大量试验和错误,我发现您不应该传递路径,并且您当然不应该包含
.dll
扩展。以下解决了我的问题:希望这能对其他人有所帮助!
In case anyone else ever has the same problem - I was also getting this error but had a slightly different problem. I was trying to load an assembly that clearly existed like the following:
After lots of trial and error I figured out that you aren't supposed to pass the path and you certainly aren't supposed to include
.dll
extension. The following fixed my issue:Hopefully that helps someone else sooner or later!
在回答我的问题之前,我必须声明上面发布的代码没有给我带来任何问题(构建错误等)。它只是给了我我在问题中指出的错误。 Unity 此时的问题是,它没有提供无法加载哪个程序集或程序集中的哪些类型。这是请求的功能。
就我而言,这是一个缺少装配的问题。我没有将实体程序集引用到客户端应用程序项目中。似乎“实体”程序集只能在运行时解析(因为它没有给我任何编译时错误)。然而,运行时错误也根本没有用处。
我查看了 Fusion Log 查看器(它应位于 .NET SDK 文件夹中)。这是多么实用的瑰宝啊。它可以记录所有类型的程序集绑定(全部或仅失败),并且它对无法加载的程序集给出了非常简洁的描述。非常有帮助!
因此,下次您收到“给定的程序集名称或代码库无效”错误时,请尝试 Fusion Log Viewer。它不会帮助您找到无法加载的类型。但是,至少您将确保所有程序集都正确加载。
Before I answer my question, I must state that the code posted above didn't give me any problem (build error etc.). It just gave me the error I stated in my question. The problem with Unity at this point of time is that It does not provide which assembly or a which types in the assembly could not be loaded. This is a requested feature.
In my case It was a missing assembly problem. I didn't reference Entities assembly in to the client application project. It seems that that "Entities" assembly could be resolved only at the run-time (since it didn't give me any compile time error). However, the run-time error was also not useful at all.
I had a look a Fusion Log viewer (It should be in the .NET SDK folder). What a gem of an utility It is. It can log all kind of assembly bindings (all or only failures) and It give a very neat description of which assembly could not load. Very helpful!
So, next time, you get this "The given assembly name or codebase was invalid" error, try Fusion Log Viewer. It wont help you in finding which types couldn't be loaded. However,at least you will be sure all your assemblies are getting loaded correctly.
如果您连接域 AssemblyResolve 事件,您可以获得绑定失败的程序集。
If you connect up the domain AssemblyResolve event you can get the assembly that has failed to bind.