WCF还是比较常用的,于是我研究了一下WCF配置可靠性,在这里拿出来和大家分享一下,希望对大家有用。通过编程方式或管理方式都可以WCF配置可靠性(以及有序传递)。如果我们启用了可靠性,则客户端与服务宿主端必须保持一致,否则客户端无法与服务通信。我们可以只对支持它的绑定WCF配置可靠性。例所示的服务端配置文件,使用了绑定配置节,启用了TCP绑定的可靠性。
例:启用TCP绑定的可靠性
<system.serviceModel>
<services>
<service name = "MyService">
<endpoint
address = "net.tcp://localhost:8000/MyService"
binding = "netTcpBinding"
bindingConfiguration = "ReliableTCP"
contract = "IMyContract"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name = "ReliableTCP">
<reliableSession enabled = "true"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
至于编程配置方式,TCP绑定和WS绑定提供了略微不同的属性来WCF配置可靠性。例如,NetTcpBinding绑定接受一个Boolean型的构造函数参数,用来启动可靠性:
public class NetTcpBinding : Binding,...
{
public NetTcpBinding(...,bool reliableSessionEnabled);
//更多成员
}
- 1.
- 2.
- 3.
- 4.
- 5.
我们只能在对象的构造期间启用可靠性。如果通过编程方式设置可靠性,需要创建支持可靠性的绑定对象:
Binding reliableTcpBinding = new NetTcpBinding(...,true);
NetTcpBinding定义了只读的ReliableSession类,通过它获取可靠性的状态:
public class ReliableSession
{
public TimeSpan InactivityTimeout
{get;set;}
public bool Ordered
{get;set;}
//更多成员
}
public class OptionalReliableSession : ReliableSession
{
public bool Enabled
{get;set;}
//更多成员
}
public class NetTcpBinding : Binding,...
{
public OptionalReliableSession ReliableSession
{get;}
//更多成员
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
#T#理论上,服务代码和契约定义应该与它使用的绑定及属性无关。服务不应该考虑绑定,在服务代码中也不应该包含它所使用的绑定。不管配置的绑定是哪一种,服务都应该能够正常工作。然而实际上,服务的实现或者契约本身都会依赖于消息的有序传递(Ordered Delivery)。为了帮助契约或服务的开发者能够约束支持的绑定,WCF定义了DeliveryRequirementsA。