【IT168技术文档】
WCF(Windows Communication Foundation) - 可靠性消息(ReliableMessaging):
·通过重试的方法来保证消息的可靠传递,默认为8次
·当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端
服务
IReliable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Message
{
/**//// <summary>
/// 演示可靠性消息的接口
/// </summary>
/// <remarks>
/// DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求
/// RequireOrderedDelivery - 如果指示 WCF 确认绑定必须支持排序消息,则为 true;否则为 false。默认值为 false。如果设置为了 true,那么也需要在配置的时候将order设置为 true
/// QueuedDeliveryRequirements - 指定服务的绑定是否必须支持排队协定
/// QueuedDeliveryRequirementsMode.Allowed - 允许排队传送
/// QueuedDeliveryRequirementsMode.Required - 要求排队传送
/// QueuedDeliveryRequirementsMode.NotAllowed - 不允许排队传送
/// </remarks>
[ServiceContract]
[DeliveryRequirements(RequireOrderedDelivery = true, QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Required)]
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;
using System.ServiceModel;
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();
}
}
}