我在尝试使用 Unity 实现示例 WCF 依赖注入时遇到此错误。
Resolution of the dependency failed, type = "SampleServiceUsingUnity.ExampleService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, ExampleServiceUsingUnity.IExampleManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving SampleServiceUsingUnity.ExampleService,(none)
Resolving parameter "exampleManager" of constructor SampleServiceUsingUnity.ExampleService(SampleServiceUsingUnity.IExampleManager exampleManager)
Resolving SampleServiceUsingUnity.IExampleManager,(none)
问题是,我什至尝试显式映射类型......当我调试代码时,我会转到即时窗口并查看注册。见下文:
**DependencyFactory.Container.Registrations.ToList()[2]**
{Microsoft.Practices.Unity.ContainerRegistration}
buildKey: {Build Key[SampleServiceUsingUnity.IExampleRepository, null]}
LifetimeManager: {Microsoft.Practices.Unity.ContainerControlledLifetimeManager}
LifetimeManagerType: {Name = "ContainerControlledLifetimeManager" FullName = "Microsoft.Practices.Unity.ContainerControlledLifetimeManager"}
MappedToType: {Name = "ExampleRepository" FullName = "SampleServiceUsingUnity.ExampleRepository"}
Name: null
RegisteredType: {Name = "IExampleRepository" FullName = "SampleServiceUsingUnity.IExampleRepository"}
所以映射看起来是正确的,但是当服务尝试使用 Resolve 实例化该接口时,我得到了错误。
代码示例位于:http: //msdn.microsoft.com/en-us/library/hh323725.aspx