Dim myContainer As New UnityContainer
Dim myHelloWorld As IHelloWorld = myContainer.Resolve(Of IHelloWorld)()
MessageBox.Show(myHelloWorld.SayHello())
执行到第二句就出错了,告诉我:
Resolution of the dependency failed, type = "Demo.Interface.IHelloWorld", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Demo.Interface.IHelloWorld, is an interface and cannot be constructed. Are you missing a type mapping?
我的app.config文件如下:
<?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">
<alias alias="IHelloWorld" type="Demo.Interface.IHelloWorld, Demo.Interface" />
<namespace name="Demo.Interface" />
<assembly name="Demo.Interface" />
<container>
<register type="IHelloWorld" mapTo="Demo.Instance.MyHelloWorld, Demo.Instance" />
</container>
</unity>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
补充:
Demo.Interface.dll文件里面是Demo.Interface.IHelloWorld
Demo.Instance.dll文件里面是实现 Demo.Instance.MyHelloWorld
我的主程序是Demo.Unity.exe
UnityContainer.LoadConfiguration
Unity 的帮助文档上有,你应该先看看。
Microsoft的实例也是坑人,所有Google到的代码都是显式声明。
LoadConfiguration居然在另外一个DLL里面。
你虽然说得不清不楚,不过给了点提示,谢谢。