当前位置:编程学习 > 网站相关 >>

化零为整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;

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

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();
        }
    }
}

 

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 - 与此绑定一起使用的安全设置-->
                    <!--mo

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,