本节主要内容:1.如何读取Binding中的binding元素。2.CustomBinding元素的基本配置。3.代码示例
一、Bingding是由binding元素构成的,可以根据实际需要,进行适当的选择进行组合。下面的一段代码可以帮助你查看Binding中包含的binding元素。
1 NetTcpBinding binding = new NetTcpBinding();
2 foreach (BindingElement element in binding.CreateBindingElements())
3 {
4 Console.WriteLine(element.GetType().FullName);
5 }
查询到netTcpBinidng的结果如下:
二、对CustomBinding进行配置。如下
服务端:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfCustomBinding.Host.Services.PersonService">
<endpoint address="net.tcp://localhost:6666/personService"
binding="netTcpBinding"
contract="WcfCustomBinding.Host.Services.IPerson"></endpoint>
<endpoint address="net.tcp://localhost:7777/personService"
binding="customBinding"
bindingConfiguration="personBinding"
contract="WcfCustomBinding.Host.Services.IPerson">
</endpoint>
</service>
</services>
<bindings>
<customBinding>
<binding name="personBinding">
<binaryMessageEncoding></binaryMessageEncoding>
<tcpTransport></tcpTransport>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>
客户端:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="Nettcp"
address="net.tcp://localhost:6666/personService"
binding="netTcpBinding"
contract="WcfCustomBinding.Client.Proxys.IPerson">
</endpoint>
<endpoint name="Custom"
address="net.tcp://localhost:7777/personService"
binding="customBinding"
bindingConfiguration="personBinding"
contract="WcfCustomBinding.Client.Proxys.IPerson">
</endpoint>
</client>
<bindings>
<customBinding>
<binding name="personBinding">
<binaryMessageEncoding></binaryMessageEncoding>
<tcpTransport></tcpTransport>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>
三、示例:
本示例采用了netTcpBinding的默认设置和自定义的CustomBinding这两种方式,而且在客户端还对其性能进行了简单的比较。为了更好的比较两种性能,在客户端最好能够多运行几次进行比较。运行结果如下:其实可以通过更改netTcpBinding的配置文件,达到CustomBinding同样的性能。
代码这里下载