Ash*_*pta 13 c# enterprise-library unity-container
接口(在名为"Interfaces"的程序集中.在项目中: - 接口)
namespace Interfaces
{
public interface IDoSomeWork1
{
string DoSomeWork1();
}
}
namespace Interfaces
{
public interface IDoSomeWork2
{
string DoSomeWork2();
}
}
Run Code Online (Sandbox Code Playgroud)
依赖关系(在名为"实体"的程序集中.在项目中: - 实体)
namespace Entities
{
public class ClassB : IDoSomeWork1
{
public string DoSomeWork1()
{
return this.ToString();
}
}
}
namespace Entities
{
public class ClassC : IDoSomeWork2
{
public string DoSomeWork2()
{
return this.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
类(在项目中: - 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());
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
客户端(在控制台应用程序项目中: - 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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行客户端时,我在section.Configure(container);得到以下错误: -
给定的程序集名称或代码库无效.(HRESULT异常:0x80131047)
我不确定配置或类型是否有任何问题.谁能指出这里的错误?
dre*_*w_w 19
如果其他人遇到同样的问题 - 我也遇到了这个错误,但问题略有不同.我试图加载一个明显存在的组件,如下所示:
Assembly.Load("C:\\Program Files\\MyProgram\\MyAssembly.dll");
Run Code Online (Sandbox Code Playgroud)
经过大量的试验和错误,我发现你不应该通过这条路,你当然不应该包括.dll扩展.以下修复了我的问题:
Assembly.Load("MyAssembly");
Run Code Online (Sandbox Code Playgroud)
希望迟早能帮助别人!
Ash*_*pta 15
在回答我的问题之前,我必须说明上面发布的代码没有给我任何问题(构建错误等).它只是给了我在我的问题中所说的错误.Unity在这个时间点的问题是它没有提供哪个程序集或程序集中的哪些类型无法加载.这是一项要求的功能.
在我的情况下,这是一个缺少装配问题.我没有将实体程序集引用到客户端应用程序项目中.似乎"实体"程序集只能在运行时解析(因为它没有给我任何编译时错误).但是,运行时错误根本没用.
我看了一下Fusion Log查看器(它应该在.NET SDK文件夹中).它是一个实用工具的宝石.它可以记录所有类型的程序集绑定(全部或仅故障),并且它给出了无法加载哪个程序集的非常简洁的描述.很有帮助!


因此,下次您收到"给定的程序集名称或代码库无效"错误,请尝试使用Fusion Log Viewer.它不会帮助您找到无法加载的类型.但是,至少您将确保所有程序集都正确加载.
如果连接域AssemblyResolve事件,则可以获取无法绑定的程序集.
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46197 次 |
| 最近记录: |