共用方式為


自定義系結傳輸和編碼

傳輸範例示範如何使用各種傳輸和訊息編碼元素來設定自定義系結。 自定義系結是由離散綁定項的已排序列表所定義。

備註

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

此範例是以 自我主機為基礎,並已修改為設定三個端點以支援具有自定義系結的 HTTP、TCP 和 NamedPipe 傳輸。 用戶端組態也進行了類似的修改,而且用戶端程式代碼已變更為與這三個端點中的每一個進行通訊。

此範例示範如何設定支援特定傳輸和訊息編碼的自定義系結。 這是藉由為 binding 元素設定傳輸和消息編碼來完成的。 綁定項的順序對於定義自定義系結很重要,因為每個系結都代表通道堆疊中的圖層(請參閱 自定義系結)。 此範例會設定三個自定義系結:具有文字編碼的 HTTP 傳輸、具有文字編碼的 TCP 傳輸,以及具有二進位編碼的 NamedPipe 傳輸。

服務組態會定義自定義系結,如下所示:

<bindings>
    <customBinding>
        <binding name="HttpBinding" >
            <textMessageEncoding
                messageVersion="Soap12Addressing10"/>
            <httpTransport />
        </binding>
        <binding name="TcpBinding" >
            <textMessageEncoding />
            <tcpTransport />
        </binding>
        <binding name="NamedPipeBinding" >
            <binaryMessageEncoding />
            <namedPipeTransport />
        </binding>
    </customBinding>
</bindings>

當您執行範例時,作業要求和回應會顯示在服務和用戶端控制台視窗中。 客戶端會與三個端點中的每一個進行通訊,首先存取 HTTP,接著 TCP,最後 NamedPipe。 在每個主控台視窗中按 ENTER 鍵,關閉服務和用戶端。

namedPipeTransport 綁定不支持機器間操作。 它僅用於相同電腦上的通訊。 因此,在跨計算機案例中執行範例時,請在用戶端程式代碼檔案中批注化下列幾行:

CalculatorClient client = new CalculatorClient("default");
Console.WriteLine("Communicate with named pipe endpoint.");
// Call operations.
DoCalculations(client);
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
Dim client As New CalculatorClient("default")
Console.WriteLine("Communicate with named pipe endpoint.")
' call operations
DoCalculations(client)
'Closing the client gracefully closes the connection and cleans up resources
client.Close()

備註

如果您使用 Svcutil.exe 重新產生此範例的組態,請務必修改用戶端組態中的端點名稱,以符合客戶端程序代碼。

要設定、建置和執行範例,請執行以下步驟:

  1. 請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。

  2. 若要建置 C#、C++ 或 Visual Basic .NET 版本的解決方案,請遵循 建置 Windows Communication Foundation 範例中的指示。

  3. 若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。