传输示例演示如何使用各种传输和消息编码元素配置自定义绑定。 自定义绑定由离散绑定元素的有序列表定义。
注释
本示例的设置过程和生成说明位于本主题末尾。
此示例基于 自承载,已修改为配置三个终结点以支持使用自定义绑定的 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 重新生成此示例的配置,请确保修改客户端配置中的终结点名称以匹配客户端代码。
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C#、C++ 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。