2017-06-20 172 views
0

我與統一註冊一個實例是這樣的:廣東話決心實例

ContentSlideControlsEntity contentSlideControlsEntity = new ContentSlideControlsEntity(new Queue<uint>()); 
     container.RegisterInstance(typeof(ContentSlideControlsEntity), "contentSlideControlsEntity", contentSlideControlsEntity); 

然後,我只是想reslove它:

ContentSlideControlsEntity contentSlideControlsEntity2 = container.Resolve<ContentSlideControlsEntity>(); 

,但我得到以下運行時錯誤:

Microsoft.Practices.Unity.ResolutionFailedException: 'Resolution of the dependency failed, type = "MSDataLayer.Entities.ContentSlideControlsEntity", name = "(none)".

Exception occurred while: while resolving.

Exception is: InvalidOperationException - The type Queue`1 has multiple constructors of length 1. Unable to disambiguate.


At the time of the exception, the container was:

Resolving MSDataLayer.Entities.ContentSlideControlsEntity,(none)

Resolving parameter "slideIDQueue" of constructor MSDataLayer.Entities.ContentSlideControlsEntity(System.Collections.Generic.Queue`1[[System.UInt32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] slideIDQueue)

Resolving System.Collections.Generic.Queue`1[System.UInt32],(none) 

回答

1

您將您的實例註冊爲已命名的註冊,但您正在解析未命名的註冊(其中d不存在)。

container.RegisterInstance(typeof(ContentSlideControlsEntity), "contentSlideControlsEntity", contentSlideControlsEntity); 
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

你有2種選擇:

1)不要把它註冊爲一個名爲registraion

container.RegisterInstance(typeof(ContentSlideControlsEntity), contentSlideControlsEntity); 

2)名稱

container.Resolve<ContentSlideControlsEntity>("contentSlideControlsEntity"); 
解決問題