1

我在尝试使用 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

4

1 回答 1

1

例外是关于 IExampleManager,而注册是关于 IExampleRepository。您是否有可能错过了管理器的映射,而是为存储库提供了一个映射?

于 2012-05-14T12:38:50.790 回答