化零为整WCF(15) - 可靠性消息(ReliableMessaging)

简介:
[索引页]
[源码下载] 


化零为整WCF(15) - 可靠性消息(ReliableMessaging)


作者: webabcd


介绍
WCF(Windows Communication Foundation) - 可靠性消息(ReliableMessaging):
    ·通过重试的方法来保证消息的可靠传递,默认为8次
    ·当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端



示例
1、服务
IReliable.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 演示可靠性消息的接口 
InBlock.gif         /// </summary> 
InBlock.gif         /// <remarks> 
InBlock.gif        /// DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求 
InBlock.gif        /// RequireOrderedDelivery - 如果指示 WCF 确认绑定必须支持排序消息,则为 true;否则为 false。默认值为false。如果设置为了 true,那么也需要在配置的时候将order设置为 true 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceContract] 
InBlock.gif        [DeliveryRequirements(RequireOrderedDelivery =  true)] 
InBlock.gif         public  interface IReliable 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 将字符串写入文本文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="str">需要写入文本文件的字符串</param> 
InBlock.gif                [OperationContract(IsOneWay =  true)] 
InBlock.gif                 void Write( string str); 
InBlock.gif        } 
InBlock.gif}
 
Reliable.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 演示可靠性消息的类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Reliable : IReliable 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 将字符串写入文本文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="str">需要写入文本文件的字符串</param> 
InBlock.gif                 public  void Write( string str) 
InBlock.gif                { 
InBlock.gif                        System.IO.StreamWriter sw =  new System.IO.StreamWriter( @"C:\WCF_Log_Reliable.txt"true); 
InBlock.gif                        sw.Write(str); 
InBlock.gif                        sw.WriteLine(); 
InBlock.gif                        sw.Close(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
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 
                <br /> 
                2、程序调用proxy.Hello("1")后马上停止Trace,过一会再打开Trace,发现程序还会调用proxy.Hello("2"); 
        </p> 
        <p> 
                备注: 
                <br /> 
                1、通过重试的方法来保证消息的可靠传递,默认为8次 
                <br /> 
                2、当配置了“有序传递”的时候,客户端和服务端会开辟缓冲区,服务端缓冲区在接到所有客户端发来的消息后,按照客户端调用的顺序排序各个消息,然后有序地调用服务端 
        </p> 
</asp:Content>
 

Reliable.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Data; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif using System.ServiceModel.Channels; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif public partial  class Message_Reliable : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         protected  void btnReliable_Click( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 using (var proxy =  new MessageSvc.Reliable.ReliableClient()) 
InBlock.gif                { 
InBlock.gif                        proxy.Write( "1"); 
InBlock.gif                        System.Threading.Thread.Sleep(3000); 
InBlock.gif                        proxy.Write( "2"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
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
[源码下载]


     本文转自webabcd 51CTO博客,原文链接 http://blog.51cto.com/webabcd/344164 :,如需转载请自行联系原作者
相关文章