2012-05-02 71 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。有沒有機會錯過管理者的映射,而是爲存儲庫提供了一個映射?

相關問題