WCF分布式开发步步为赢系列的(4):WCF服务可靠性传输配置与编程开发
【2】配置方式实现可靠性传输:
下面我们使用服务端配置文件,启用了TCP绑定的可靠性。代码如下:
<system.serviceModel>
<services>
<service behaviorConfiguration="WCFService.WCFServiceBehavior"
name="WCFService.WCFService">
<endpoint
address="http://localhost:8001/WCFService"
binding="wsHttpBinding"
contract="WCFService.IWCFService">
</endpoint>
<endpoint
address="net.tcp://localhost:8002/WCFService"
binding="netTcpBinding"
bindingConfiguration="ReliableTCP"
contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
<add baseAddress="net.tcp://localhost:8002/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding >
<binding name="ReliableTCP">
<reliableSession enabled="True"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
<services>
<service behaviorConfiguration="WCFService.WCFServiceBehavior"
name="WCFService.WCFService">
<endpoint
address="http://localhost:8001/WCFService"
binding="wsHttpBinding"
contract="WCFService.IWCFService">
</endpoint>
<endpoint
address="net.tcp://localhost:8002/WCFService"
binding="netTcpBinding"
bindingConfiguration="ReliableTCP"
contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
<add baseAddress="net.tcp://localhost:8002/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding >
<binding name="ReliableTCP">
<reliableSession enabled="True"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
这里我们要设置服务终结点的绑定配置,bindingConfiguration="ReliableTCP",然后在给出具体绑定配置的可靠传输
Code
<netTcpBinding >
<binding name="ReliableTCP">
<reliableSession enabled="True"/>
</binding>
</netTcpBinding>
这样WCF服务就会启用可靠消息传递。