我在尝试使用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
答案 0 :(得分:1)
异常是在讨论IExampleManager,而注册是关于IExampleRepository的。您是否有机会错过管理器的映射,而是为存储库提供了一个映射?