WCF 中CustomBinding的配置

本节主要内容:1.如何读取Binding中的binding元素。2.CustomBinding元素的基本配置。3.代码示例

一、Bingding是由binding元素构成的,可以根据实际需要,进行适当的选择进行组合。下面的一段代码可以帮助你查看Binding中包含的binding元素。

View Code
1  NetTcpBinding binding = new NetTcpBinding();
2 foreach (BindingElement element in binding.CreateBindingElements())
3 {
4 Console.WriteLine(element.GetType().FullName);
5 }

查询到netTcpBinidng的结果如下:

二、对CustomBinding进行配置。如下

服务端:

View Code
 1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="WcfCustomBinding.Host.Services.PersonService">
6 <endpoint address="net.tcp://localhost:6666/personService"
7 binding="netTcpBinding"
8 contract="WcfCustomBinding.Host.Services.IPerson"></endpoint>
9 <endpoint address="net.tcp://localhost:7777/personService"
10 binding="customBinding"
11 bindingConfiguration="personBinding"
12 contract="WcfCustomBinding.Host.Services.IPerson">
13 </endpoint>
14 </service>
15 </services>
16 <bindings>
17 <customBinding>
18 <binding name="personBinding">
19 <binaryMessageEncoding></binaryMessageEncoding>
20 <tcpTransport></tcpTransport>
21 </binding>
22 </customBinding>
23 </bindings>
24 </system.serviceModel>
25 </configuration>

客户端:

View Code
 1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <client>
5 <endpoint name="Nettcp"
6 address="net.tcp://localhost:6666/personService"
7 binding="netTcpBinding"
8 contract="WcfCustomBinding.Client.Proxys.IPerson">
9 </endpoint>
10
11 <endpoint name="Custom"
12 address="net.tcp://localhost:7777/personService"
13 binding="customBinding"
14 bindingConfiguration="personBinding"
15 contract="WcfCustomBinding.Client.Proxys.IPerson">
16 </endpoint>
17 </client>
18 <bindings>
19 <customBinding>
20 <binding name="personBinding">
21 <binaryMessageEncoding></binaryMessageEncoding>
22 <tcpTransport></tcpTransport>
23 </binding>
24 </customBinding>
25 </bindings>
26
27 </system.serviceModel>
28 </configuration>

三、示例:

本示例采用了netTcpBinding的默认设置和自定义的CustomBinding这两种方式,而且在客户端还对其性能进行了简单的比较。为了更好的比较两种性能,在客户端最好能够多运行几次进行比较。运行结果如下:其实可以通过更改netTcpBinding的配置文件,达到CustomBinding同样的性能。

WCF 中CustomBinding的配置

代码这里下载

你可能感兴趣的:(bind)