2010-01-12 77 views
11

我有一個使用MSMQ綁定的單向WCF服務,它使用IIS 7.0中的Windows激活服務激活。當使用NInject與WCF服務使用MSMQ綁定在WAS中託管

我是NInject的忠實粉絲,所以我一直在使用WCF的NInject擴展,這對於典型的HTTP WCF服務來說效果很好。

但是,在WAS激活服務中沒有HTTP管道,所以在綁定我的類型時我無法使用InRequestScope,因爲System.Web.HttpContext.Current爲null。當我使用WAS時,我正在努力尋找替代方案,它會給我想要的東西。 AspCompatibility模式屬性在此模式下也不起作用。

我想InThreadScope可能會奏效,但比它在執行一個單獨的線程創建的服務。

所以基本上我需要的HttpContext爲WCF相當於+是範圍我的對象在請求級別。在這個世界上是否有一些靜態對象會以相同的方式工作,或者有其他人對我可以共同攻擊的東西有什麼想法?

回答

9

什麼我實現我自己的WCF擴展Ninject 2.0之前,我知道有一個this了GitHub上。我的實現略有不同,但我沒有拿出一個解決方案爲範圍對象:

using System; 
using Ninject.Activation; 

namespace Ninject.Contrib.Wcf { 
    /// <summary> 
    /// Defines Scope Callbacks for WCF Context. 
    /// </summary> 
    public class NinjectWcfScopeCallbacks { 
    /// <summary> 
    /// Defines WCF Context scope. 
    /// </summary> 
    public static readonly Func<IContext, object> WcfContext = 
     ctx => (System.ServiceModel.OperationContext.Current != null 
       ? System.ServiceModel.OperationContext.Current. 
        InstanceContext. 
        Extensions.Find<NinjectInstanceContext>() 
       : null); 

    /// <summary> 
    /// Defines WCF Web Context scope. 
    /// </summary> 
    public static readonly Func<IContext, object> WcfWebContext = 
       ctx => System.ServiceModel.Web.WebOperationContext.Current; 
    } 
} 

爲了完整,這是我如何使用上面定義的回調:

Bind<IHelloWorldService>() 
     .To<HelloWorldService>() 
     .InScope(NinjectWcfScopeCallbacks.WcfWebContext); 

在沒有託管WCF在WAS服務,所以不知道你是否使用上面定義的WcfWebContextWcfContext,但你可以試試看看。如果WebOperationContext有效,那麼你就全部設置好了。否則,我發現事情有點複雜。您會注意到上面的代碼片段使用NinjectInstanceContext類,該類附加到OperationContext。這是我寫的一個類,它使用Ninject 2.0的「緩存和收集」機制,允許對象進行確定性處置。基本上,這個類實現了IExtension<InstanceContext>這是一個WCF結構,用於將幾乎所有東西都附加到OperationContext。這個類也實現Ninject的INotifyWhenDisposed接口,它爲確定性處理提供支持。下面是類的定義是什麼樣子:

/// <summary> 
    /// Defines a custom WCF InstanceContext extension that resolves service instances 
    /// using Ninject. 
    /// <remarks> 
    /// The custom InstanceContext extension provides support for deterministic disposal 
    /// of injected dependencies and service instances themselves by being hook into 
    /// Ninject's "cache and collect" mechanism (new in Ninject 2.0) for object life cycle 
    /// management. This allows binding object instances to the lifetime of a WCF context 
    /// and having them deterministically deactivated and disposed. 
    /// </remarks> 
    /// </summary> 
    public class NinjectInstanceContext : 
       IExtension<InstanceContext>, INotifyWhenDisposed { 
    } 

我的WCF擴展Ninject的其餘部分是一樣的one GitHub上。基本上發生的是創建一個插入WCF「激活」鏈的實例提供程序 - 我沒有使用它們的特定術語,而是我如何理解事情。所以,這個想法是你的實例提供者應該提供被請求的WCF服務類的實例。所以,這裏是我們使用Ninject生成服務實例的地方。通過這樣做,我們也可以激活並注入任何依賴項。實例提供程序在我的實現中執行的操作是在實例中包裝Ninject內核,如果NinjectInstanceContext已附加到OperationContext。然後將該服務的創建委託給此WCF擴展。當實例提供者被告知釋放一個服務時,被附加到OperationContext的NinjectInstanceContext被處置,通過實現INotifyWhenDisposed的方式導致服務(以及潛在的依賴)的確定性處置。

希望這個討論會有所幫助。如果您有興趣,我會看看是否可以在這裏發佈更具體的代碼。

+1

斷開的鏈接。它是否正確? https://github.com/ninject/ninject.extensions.wcf – 2011-10-02 22:52:32

+0

你是對的 - 我糾正了鏈接。 – 2011-10-03 00:54:59

0

我敢肯定OperationContext是你要找的