0

我目前正在使用統一容器和WCF服務的PRISM應用程序。 在模塊(與WCF代理),我註冊的ChannelFactory爲WCF客戶端如下:統一無法解析類型,當在一個不同的模塊解析

InstanceContext instanceContext = new InstanceContext(new TradingPlatformCallback()); 
unityContainer.RegisterType<DuplexChannelFactory<IGenericTradingInterface>, DuplexChannelFactory<IGenericTradingInterface>>(
    new ContainerControlledLifetimeManager(), 
    new InjectionConstructor(
     instanceContext, 
     "NetNamedPipeBinding_IGenericTradingInterface")); 

DuplexChannelFactory<IGenericTradingInterface> factory = unityContainer.Resolve<DuplexChannelFactory<IGenericTradingInterface>>(); 

factory.Open(); 
IGenericTradingInterface test = factory.CreateChannel(); 
test.GetServerInformation(); 
factory.Close(); 

現在,一切工作正常,所以我決定在另一個模塊中使用這個的ChannelFactory。 這裏是模塊的初始化方法:

var test = unityContainer.Resolve<DuplexChannelFactory<IGenericTradingInterface>>(); 
test.Open(); 

var test2 = test.CreateChannel(); 
test2.GetServerInformation(); 
test.Close(); 

所以這段代碼是完全相同爲其他模塊,除了缺少註冊。

運行此,我得到以下異常:

Exception is: InvalidOperationException - The type DuplexChannelFactory`1 has mu 
ltiple constructors of length 3. Unable to disambiguate. 

這似乎是與拆分和的ChannelFactory的構建函數,但團結爲什麼可以解決工廠第一模塊中,而不是一個問題在這一個?

我也弄不明白這個異常消息,因爲我認爲在與註冊已經稱爲一個特定的構造函數:

new InjectionConstructor(
       instanceContext, 
       "NetNamedPipeBinding_IGenericTradingInterface") 

任何想法?

回答

0

事實證明,問題在於模塊初始化的順序。第二個模塊被稱爲第一個,所以Unity以CTor的參數最多,而DuplexChannelFactory最多隻有3個,其中很多都是。謝謝,Juergen

1

你不顯示如何(或是否)統一容器跨模塊共享。基於你的變量名稱(「unityContainer」),我猜這是模塊內的局部變量?這意味着你有兩個獨立的容器實例,每個實例都需要註冊。