2011-07-15 76 views
2

我正在使用Ninject 2.2.1.4和Ninject.Extensions.Wcf 2.2.0.4。如何將Ninject WCF擴展與單例服務結合使用?

我的服務都裝飾有以下屬性:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

使用上面的屬性,我收到錯誤:

"The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host."

如果我刪除屬性Ninject按預期工作。

經過一番研究,我的理解是,由於服務被標記爲單例,它將自動使用ServiceHost的無參數構造函數創建,因此是錯誤消息。爲了解決這個問題,我必須自己創建服務對象,使用ninject解析,然後將該單例對象傳遞給ServiceHost以供使用。我不知道該怎麼做。

我看着WCF擴展源,我看到NinjectServiceHost從ServiceHost的繼承,所以我的想法是使用重載的構造函數NinjectServiceHost綁定到我的服務對象的實例:

public NinjectServiceHost(object singletonInstance) 
     : base(singletonInstance) 
    { 
    } 

我不是確定它是否正確,如果是,如何以及在哪裏正確綁定它,以便ServiceHost可以餵食我的實例。

有什麼建議嗎?謝謝。

回答

1

如果您可以使用測試版本,我建議更新到2.3.x它支持IIS託管的單件服務。另請參閱github上的示例

+0

謝謝,我會研究一下。你知道它什麼時候會出現beta版嗎? – Mike

+0

不,我不提供任何日期。雖然我確切知道下一個版本還有多少工作要做,但我完全不知道我能花多少時間在Ninject上。 但是關於WCF,不應該改變任何東西。添加到WCF的其他程序集的唯一方法是支持用於XML和約定擴展的InRequestScope。 –