2012-05-02 55 views
1

我收到了这个错误,试图用Unity实现示例WCF依赖注入。正确映射的Unity异常解析类型。为什么?

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"} 

所以映射看起来正确,但是当服务试图实例化操作界面采用解决,我得到的错误。

的代码示例在被提供: http://msdn.microsoft.com/en-us/library/hh323725.aspx

+0

这个例外是关于'IExampleManager',而注册是关于'IExampleRepository'。有没有机会错过管理者的映射,而是为存储库提供了一个映射? –

+0

我需要在帖子的开头修正错误,并会很快完成。显然我通过尝试不同的东西来贴上类似的例外。代码行如下,并且当服务被实例化时它们被调用... DependencyFactory.Container.RegisterType (new ContainerControlledLifetimeManager()); DependencyFactory.Container.RegisterType(typeof(ExampleRepository)); –

+0

不是你说得对。我搞砸了。我非常忙于关注ExampleService和ExampleRepository,我甚至没有注意到它是在说ExampleManager。 (猜测我的眼睛模糊了例子这个词)。我刚刚为同侪编程提出了一个很好的论点,不是吗?谢谢! –

回答

1

异常被谈论IExampleManager而注册约为IExampleRepository。有没有机会错过管理者的映射,而是为存储库提供了一个映射?

相关问题