化零为整WCF(15) - 可靠性消息(ReliableMessaging)
[索引页]
[源码下载]
作者:webabcd
介绍
WCF(Windows Communication Foundation) - 可靠性消息(ReliableMessaging):
·通过重试的方法来保证消息的可靠传递,默认为8次
·当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端
示例
1、服务
IReliable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.ServiceModel;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Message
{
/// <summary>
/// 演示可靠性消息的接口
/// </summary>
/// <remarks>
/// DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求
/// RequireOrderedDelivery - 如果指示 WCF 确认绑定必须支持排序消息,则为 true;否则为 false。默认值为 false。如果设置为了 true,那么也需要在配置的时候将order设置为 true
/// </remarks>
[ServiceContract]
[DeliveryRequirements(RequireOrderedDelivery = true)]
public interface IReliable
{
/// <summary>
/// 将字符串写入文本文件
/// </summary>
/// <param name="str">需要写入文本文件的字符串</param>
[OperationContract(IsOneWay = true)]
void Write(string str);
}
}
Reliable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.ServiceModel;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Message
{
/// <summary>
/// 演示可靠性消息的类
/// </summary>
public class Reliable : IReliable
{
/// <summary>
/// 将字符串写入文本文件
/// </summary>
/// <param name="str">需要写入文本文件的字符串</param>
public void Write(string str)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\WCF_Log_Reliable.txt", true);
sw.Write(str);
sw.WriteLine();
sw.Close();
}
}
}
2、宿主
Reliable.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Message.Reliable" %>
Web.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<!--name - 提供服务的类名-->
<!--behaviorConfiguration - 指定相关的行为配置-->
<service name="WCF.ServiceLib.Message.Reliable" behaviorConfiguration="MessageBehavior">
<!--address - 服务地址(监听地址);listenUri - 服务监听地址(实际地址)。监听可以在host中设置(本例),也可以在client中设置(参看MTOM的例子)-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<!--bindingConfiguration - 指定相关的绑定配置-->
<endpoint address="http://localhost:8888/ServiceHost/Message/Reliable.svc" listenUri="http://localhost:3502/ServiceHost/Message/Reliable.svc" binding="wsHttpBinding" contract="WCF.ServiceLib.Message.IReliable" bindingConfiguration="ReliableBindingConfiguration" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MessageBehavior">
<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="ReliableBindingConfiguration">
<!--reliableSession - 对可靠会话绑定元素属性的设置-->
<!--enabled - 指示是否在通道终结点之间建立 WS-RM (WS-ReliableMessaging) 可靠会话。默认值为 false-->
<!--ordered - 该值指示消息传递是否必须保持与消息发送一致的顺序(如果设置为true,那么也需要在相应的接口或类上声明DeliveryRequirements)-->
<!--inactivityTimeout - 服务在关闭之前保持非活动状态的时间间隔-->
<reliableSession enabled="true" ordered="true" inactivityTimeout="00:10:00" />
<!--security - 与此绑定一起使用的安全设置-->
<!--mode="None" - 禁用安全性-->
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
3、客户端
Reliable.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Reliable.aspx.cs"
Inherits="Message_Reliable" Title="可靠性消息(ReliableMessaging)" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Button ID="btnReliable" runat="server" Text="可靠性消息测试" OnClick="btnReliable_Click" />
<p>
测试方法:
<br />
1、用TcpTrace监听8888端口,目标端口3502
<br />
2、程序调用proxy.Hello("1")后马上停止Trace,过一会再打开Trace,发现程序还会调用proxy.Hello("2");
</p>
<p>
备注:
<br />
1、通过重试的方法来保证消息的可靠传递,默认为8次
<br />
2、当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端
</p>
</asp:Content>
Reliable.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ServiceModel.Channels;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class Message_Reliable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnReliable_Click(object sender, EventArgs e)
{
using (var proxy = new MessageSvc.Reliable.ReliableClient())
{
proxy.Write("1");
System.Threading.Thread.Sleep(3000);
proxy.Write("2");
}
}
}
Web.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<!--bindingConfiguration - 指定相关的绑定配置-->
<endpoint address="http://localhost:8888/ServiceHost/Message/Reliable.svc" binding="wsHttpBinding" contract="MessageSvc.Reliable.IReliable" bindingConfiguration="ReliableBindingConfiguration" />
</client>
<bindings>
<wsHttpBinding>
<binding name="ReliableBindingConfiguration">
<!--reliableSession - 对可靠会话绑定元素属性的设置-->
<!--enabled - 指示是否在通道终结点之间建立 WS-RM (WS-ReliableMessaging) 可靠会话。默认值为 false-->
<!--ordered - 该值指示消息传递是否必须保持与消息发送一致的顺序(如果设置为true,那么也需要在相应的接口或类上声明DeliveryRequirements)-->
<!--inactivityTimeout - 服务在关闭之前保持非活动状态的时间间隔-->
<reliableSession enabled="true" ordered="true" inactivityTimeout="00:10:00" />
<!--security - 与此绑定一起使用的安全设置-->
<!--mode="None" - 禁用安全性-->
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
运行结果:
1、用TcpTrace监听8888端口,目标端口3502
2、程序调用proxy.Hello("1")后马上停止Trace,过一会再打开Trace,发现程序还会调用proxy.Hello("2");
OK
[源码下载]
[源码下载]
化零为整WCF(15) - 可靠性消息(ReliableMessaging)
作者:webabcd
介绍
WCF(Windows Communication Foundation) - 可靠性消息(ReliableMessaging):
·通过重试的方法来保证消息的可靠传递,默认为8次
·当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端
示例
1、服务
IReliable.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Reliable.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、宿主
Reliable.svc
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Web.config
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3、客户端
Reliable.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Reliable.aspx.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Web.config
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果:
1、用TcpTrace监听8888端口,目标端口3502
2、程序调用proxy.Hello("1")后马上停止Trace,过一会再打开Trace,发现程序还会调用proxy.Hello("2");
OK
[源码下载]