【问题标题】:Unity Exception resolving type that is correctly mapped. Why?正确映射的 Unity Exception 解析类型。为什么?
【发布时间】:2012-05-02 15:54:15
【问题描述】:

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

【问题讨论】:

  • 例外是关于IExampleManager,而注册是关于IExampleRepository。您是否有可能错过了管理器的映射,而是为存储库提供了一个映射?
  • 我需要修复帖子开头的错误,很快就会完成。显然,我在尝试不同的东西时粘贴了一个类似的例外。代码行如下,它们在服务实例化时被调用... DependencyFactory.Container.RegisterType(new ContainerControlledLifetimeManager()); DependencyFactory.Container.RegisterType(typeof(ExampleRepository));
  • 不,你做对了。我搞砸了。我忙于专注于 ExampleService 和 ExampleRepository,我什至没有注意到它在说 ExampleManager。 (猜我的眼睛被示例这个词弄糊涂了)。我刚刚为对等编程提出了很好的论据,不是吗?谢谢!
  • 想发布您的评论作为答案@SebastianWeber?将允许将问题标记为已回答。我会发布你所说的作为答案,但这样做感觉不对:)

标签: wcf interface unity-container resolve


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多