<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引頁]</a>
化零為整WCF(15) - 可靠性消息(ReliableMessaging)
介紹
WCF(Windows Communication Foundation) - 可靠性消息(ReliableMessaging):
·通過重試的方法來保證消息的可靠傳遞,預設為8次
·當配置了“有序傳遞”的時候,用戶端和服務端會開辟緩沖區,服務端緩沖區在接到所有用戶端發來的消息後,按照用戶端調用的順序排序各個消息,然後有序地調用服務端
示例
1、服務
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
/// </remarks>
[ServiceContract]
[DeliveryRequirements(RequireOrderedDelivery = true)]
public interface IReliable
{
/// <summary>
/// 将字元串寫入文本檔案
/// </summary>
/// <param name="str">需要寫入文本檔案的字元串</param>
[OperationContract(IsOneWay = true)]
void Write(string str);
}
}
Reliable.cs
/// 示範可靠性消息的類
public class Reliable : IReliable
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)" %>
<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
2、程式調用proxy.Hello("1")後馬上停止Trace,過一會再打開Trace,發現程式還會調用proxy.Hello("2");
</p>
備注:
1、通過重試的方法來保證消息的可靠傳遞,預設為8次
2、當配置了“有序傳遞”的時候,用戶端和服務端會開辟緩沖區,服務端緩沖區在接到所有用戶端發來的消息後,按照用戶端調用的順序排序各個消息,然後有序地調用服務端
</asp:Content>
Reliable.aspx.cs
using System.Collections;
using System.Configuration;
using System.Data;
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;
public partial class Message_Reliable : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
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");
<client>
<!--address - 服務位址-->
<!--binding - 通信方式-->
<!--contract - 服務契約-->
<!--bindingConfiguration - 指定相關的綁定配置-->
<endpoint address="http://localhost:8888/ServiceHost/Message/Reliable.svc" binding="wsHttpBinding" contract="MessageSvc.Reliable.IReliable" bindingConfiguration="ReliableBindingConfiguration" />
</client>
運作結果:
1、用TcpTrace監聽8888端口,目标端口3502
2、程式調用proxy.Hello("1")後馬上停止Trace,過一會再打開Trace,發現程式還會調用proxy.Hello("2");
OK
本文轉自webabcd 51CTO部落格,原文連結http://blog.51cto.com/webabcd/344164:,如需轉載請自行聯系原作者