自定义绑定传输和编码

自定义绑定由离散绑定元素的有序列表定义。本示例演示如何使用各种传输和消息编码元素配置自定义绑定。

提示

本主题的末尾介绍了此示例的设置过程和生成说明。

本示例基于自承载,为了配置用于使用自定义绑定支持 HTTP、TCP 和 NamedPipe 传输的三个终结点,已经对该示例做了修改,并对客户端配置进行了类似修改,将客户端代码更改为能与这三个终结点中的每一个进行通信。

本示例演示如何配置支持特定传输和消息编码的自定义绑定。这是通过为 binding 元素配置传输和消息编码而实现的。在定义自定义绑定时,绑定元素的排序顺序很重要,因为每个元素都表示通道堆栈中的某一层(请参见Custom Bindings)。本示例配置三个自定义绑定:使用文本编码的 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 示例的一次性安装过程

  2. 若要生成 C#、C++ 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要用单机配置或跨计算机配置来运行示例,请按照运行 Windows Communication Foundation 示例中的说明进行操作。

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.