Share via


自訂訊息攔截器

這個 MessageInterceptor 範例 (英文) 示範通道擴充性模型的使用方式。 尤其,這個範例會示範如何實作建立通道處理站和通道接聽程式的自訂繫結項目,以攔截執行階段堆疊中特定點的所有傳入與傳出訊息。 範例也包含用戶端和伺服器,以示範這些自訂處理站的使用方式。

在這個範例中,用戶端和服務都是主控台程式 (.exe)。 用戶端和服務都會使用含有自訂繫結項目及其相關執行階段物件的通用程式庫 (.dll)。

注意

此範例的安裝程序與建置指示位於本主題的結尾。

這個範例說明使用通道架構並依照 WCF 最佳做法,在 Windows Communication Foundation (WCF) 中建立自訂層次通道的建議程序。 建立自訂層次通道的步驟如下:

  1. 決定通道處理站和通道接聽項所要支援的通道類型。

  2. 建立支援您通道類型的通道處理站和通道接聽項。

  3. 新增繫結項目,而此繫結項目會將自訂層次通道新增至通道堆疊。

  4. 新增繫結元素延伸區段,即可將新的繫結元素公開至組態系統。

通道形狀

撰寫自訂層次通道時的第一個步驟是,決定通道所需要的類型。 對於訊息偵測器,我們支援下面層次所支援的任何類型 (例如,如果下面的層次可以建置 IOutputChannelIDuplexSessionChannel,則也會公開 IOutputChannelIDuplexSessionChannel)。

通道處理站和接聽項處理站

撰寫自訂層次通道的下一個步驟是,建立用戶端通道的 IChannelFactory 實作以及服務通道的 IChannelListener 實作。

這些類別會接受內部處理站和接聽項,並且將除了 OnCreateChannelOnAcceptChannel 以外的所有呼叫委派至內部處理站和接聽項。

class InterceptingChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
{
    //...
}

class InterceptingChannelListener<TChannel> : ListenerFactoryBase<TChannel>
{
    //...
}

新增繫結項目

範例會定義自訂繫結項目:InterceptingBindingElementInterceptingBindingElementChannelMessageInterceptor 當作輸入內容,並使用這個 ChannelMessageInterceptor 來操作通過它的訊息。 這是唯一必須為公用的類別。 處理站、接聽項和通道全部都可以是公用執行階段介面的內部實作。

public class InterceptingBindingElement : BindingElement
{
}

新增組態支援

為了與繫結組態整合,程式庫會將組態區段處理常式定義為繫結項目延伸區段。 用戶端和伺服器組態檔必須向組態系統註冊繫結項目延伸。 想要將其繫結項目公開給組態系統的實作器都可以衍生自這個類別。

public abstract class InterceptingElement : BindingElementExtensionElement
{
    //...
}

新增原則

為了與原則系統整合,InterceptingBindingElement 會實作 IPolicyExportExtension 以表示必須參與產生原則。 若要在產生的用戶端上支援匯入原則,使用者可以註冊 InterceptingBindingElementImporter 的衍生類別,並覆寫 CreateMessageInterceptor() 來產生啟用原則的 ChannelMessageInterceptor 類別。

範例:可卸除的訊息偵測器

範例中包含了會卸除訊息的 ChannelMessageInspector 的範例實作。

class DroppingServerElement : InterceptingElement
{
    protected override ChannelMessageInterceptor CreateMessageInterceptor()
    {
        return new DroppingServerInterceptor();
    }
}

您可以從組態中存取,如下所示:

<configuration>
    ...
    <system.serviceModel>
        ...
        <extensions>
            <bindingElementExtensions>
                <add name="droppingInterceptor"
                   type=
          "Microsoft.ServiceModel.Samples.DroppingServerElement, library"/>
            </bindingElementExtensions>
        </extensions>
    </system.serviceModel>
</configuration>

用戶端和伺服器都會使用這個新建立的組態區段,將自訂處理站插入至執行階段通道堆疊的最下一層 (在傳輸層級之上)。

<customBinding>
  <binding name="sampleBinding">
    <droppingInterceptor/>
    <httpTransport/>
  </binding>
</customBinding>

用戶端會使用 MessageInterceptor 程式庫,將自訂標頭新增至偶數編號的訊息。 另一方面,服務則會使用 MessageInterceptor 程式庫來卸除任何沒有這個特殊標頭的訊息。

在依序執行服務和用戶端之後,您應該看見下列用戶端輸出。

Reporting the next 10 wind speed
100 kph
Server dropped a message.
90 kph
80 kph
Server dropped a message.
70 kph
60 kph
Server dropped a message.
50 kph
40 kph
Server dropped a message.
30 kph
20 kph
Server dropped a message.
10 kph
Press ENTER to shut down client

用戶端會向服務報告 10 種不同的風速,但是只將其中半數標記以特殊標頭。

您應該會在服務上看見下列輸出:

Press ENTER to exit.
Dangerous wind detected! Reported speed (90) is greater than 64 kph.
Dangerous wind detected! Reported speed (70) is greater than 64 kph.
5 wind speed reports have been received.

若要安裝、建置及執行範例

  1. 請使用下列命令安裝 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 確定您已執行 Windows Communication Foundation 範例的一次性安裝程序

  3. 若要建置解決方案,請依照建置 Windows Communication Foundation 範例中的指示操作。

  4. 若要在單一或多部電腦組態中執行此範例,請遵循執行 Windows Communication Foundation 範例中的指示進行。

  5. 先執行 Service.exe,然後執行 Client.exe,再查看兩個主控台視窗上的輸出。