天天看点

化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)

<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引页]</a>

化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)

介绍

WCF(Windows Communication Foundation) - 消息队列(MSMQ - MicroSoft Message Queue):

    netMsmqBinding的binding属性配置如下:

    ·ExactlyOnce - 确保消息只被投递一次。只能应用于事务型队列,默认值 ture

    ·Durable - 消息是否需要持久化。默认值 enabled,如果设置为disable,当MSMQ服务重启后,先前保存在MSMQ中的消息将会丢失

    ·TimeToLive - 消息过期并且从原有的队列移动到死信队列的时间。默认值 1.00:00:00 (1天)

    ·ReceiveRetryCount - 配置队列管理器在一定重试间隔中,尝试重新投递消息的次数,也就是将消息传输到重试队列前尝试发送该消息的最大次数(每隔RetryCycleDelay的时间重试ReceiveRetryCount次)。缺省值 5

    ·MaxRetryCycles - 配置队列管理器重新投递消息的重试间隔数(执行RetryCycleDelay的次数),也就是重试最大周期数。缺省值 2

    ·RetryCycleDelay - 表示两次重试之间的间隔时间,也就是重试周期之间的延迟。缺省值 00:30:00

    ·ReceiveErrorHandling - 指定如何处理错误的消息。Fault、Drop、Reject或Move(具体说明查MSDN)

    ·DeadLetterQueue - 指定所使用的死信队列的类型。None、System、或Custom(具体说明查MSDN)

    ·CustomDeadLetterQueue - 本地自定义死信队列的URI

示例

1、服务

IMSMQ.cs

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.ServiceModel; 

namespace WCF.ServiceLib.Message 

        /// &lt;summary&gt; 

        /// 演示MSMQ的接口 

        /// &lt;/summary&gt; 

        /// &lt;remarks&gt; 

        /// DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求 

        /// QueuedDeliveryRequirements - 指定服务的绑定是否必须支持排队协定 

        /// QueuedDeliveryRequirementsMode.Allowed - 允许排队传送 

        /// QueuedDeliveryRequirementsMode.Required - 要求排队传送 

        /// QueuedDeliveryRequirementsMode.NotAllowed - 不允许排队传送 

        /// &lt;/remarks&gt; 

        [ServiceContract] 

        [DeliveryRequirements(QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Required)] 

        public interface IMSMQ 

        { 

                /// &lt;summary&gt; 

                /// 将字符串写入文本文件 

                /// &lt;/summary&gt; 

                /// &lt;param name="str"&gt;需要写入文本文件的字符串&lt;/param&gt; 

                /// &lt;remarks&gt; 

                /// 如果要使用 MSMQ 的话,则必须配置IsOneWay = true 

                /// &lt;/remarks&gt; 

                [OperationContract(IsOneWay = true)] 

                void Write(string str); 

        } 

}

MSMQ.cs

        /// 演示MSMQ的类 

        public class MSMQ : IMSMQ 

                public void Write(string str) 

                { 

                        System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\WCF_Log_MSMQ.txt", true); 

                        sw.Write(str); 

                        sw.WriteLine(); 

                        sw.Close(); 

                } 

2、宿主

// 队列名 

// 只能使用.\private$\YourPrivateMSMQName来访问本机的私有MSMQ队列 

string queueName = @".\private$\SampleMSMQ"; 

// 没有queueName队列,则创建queueName队列 

if (!System.Messaging.MessageQueue.Exists(queueName)) 

        // 第二个参数为是否创建事务性队列 

        System.Messaging.MessageQueue.Create(queueName, true); 

using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.MSMQ))) 

        host.Open(); 

        Console.WriteLine("服务已启动(WCF.ServiceLib.Message.MSMQ)"); 

        Console.WriteLine("按&lt;ENTER&gt;停止服务"); 

        Console.ReadLine(); 

App.config

&lt;?xml version="1.0" encoding="utf-8" ?&gt; 

&lt;configuration&gt; 

        &lt;system.serviceModel&gt; 

                &lt;services&gt; 

                        &lt;!--name - 提供服务的类名--&gt; 

                        &lt;!--behaviorConfiguration - 指定相关的行为配置--&gt; 

                        &lt;service name="WCF.ServiceLib.Message.MSMQ" behaviorConfiguration="MessageBehavior"&gt; 

                                &lt;!--address - 服务地址--&gt; 

                                &lt;!--binding - 通信方式--&gt; 

                                &lt;!--contract - 服务契约--&gt; 

                                &lt;!--bindingConfiguration - 指定相关的绑定配置--&gt; 

                                &lt;endpoint address="" binding="netMsmqBinding" contract="WCF.ServiceLib.Message.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" /&gt; 

                                &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; 

                                &lt;host&gt; 

                                        &lt;baseAddresses&gt; 

                                                &lt;add baseAddress="http://localhost:12345/Message/MSMQ"/&gt; 

                                                &lt;add baseAddress="net.msmq://localhost/private/SampleMSMQ"/&gt; 

                                        &lt;/baseAddresses&gt; 

                                &lt;/host&gt; 

                        &lt;/service&gt; 

                &lt;/services&gt; 

                &lt;behaviors&gt; 

                        &lt;serviceBehaviors&gt; 

                                &lt;behavior name="MessageBehavior"&gt; 

                                        &lt;!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false--&gt; 

                                        &lt;serviceMetadata httpGetEnabled="true" /&gt; 

                                        &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt; 

                                &lt;/behavior&gt; 

                        &lt;/serviceBehaviors&gt; 

                &lt;/behaviors&gt; 

                &lt;bindings&gt; 

                        &lt;netMsmqBinding&gt; 

                                &lt;binding name="MSMQBindingConfiguration"&gt; 

                                        &lt;security&gt; 

                                                &lt;!--msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain --&gt; 

                                                &lt;!--MsmqAuthenticationMode.None - 不使用任何安全性--&gt; 

                                                &lt;!--MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域--&gt; 

                                                &lt;!--MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中--&gt; 

                                                &lt;!--msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign --&gt; 

                                                &lt;!--ProtectionLevel.None - 只做身份验证--&gt; 

                                                &lt;!--ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性--&gt; 

                                                &lt;!--ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性--&gt; 

                                                &lt;transport msmqAuthenticationMode="None" msmqProtectionLevel="None" /&gt; 

                                                &lt;!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName --&gt; 

                                                &lt;!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证--&gt; 

                                                &lt;!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证--&gt; 

                                                &lt;message clientCredentialType="UserName" /&gt; 

                                        &lt;/security&gt; 

                                &lt;/binding&gt; 

                        &lt;/netMsmqBinding&gt; 

                &lt;/bindings&gt; 

        &lt;/system.serviceModel&gt; 

&lt;/configuration&gt;

3、客户端

using System.Windows.Forms; 

namespace Client2.Message 

        /// 演示Message.MSMQ的类 

        public class MSMQ 

                /// 用于测试 MSMQ 的客户端 

                public void HelloMSMQ(string str) 

                        using (var proxy = new MessageSvc.MSMQ.MSMQClient()) 

                        { 

                                proxy.Write(str); 

                        } 

                }             

                &lt;client&gt; 

                        &lt;!--address - 服务地址--&gt; 

                        &lt;!--binding - 通信方式--&gt; 

                        &lt;!--contract - 服务契约--&gt; 

                        &lt;!--bindingConfiguration - 指定相关的绑定配置--&gt; 

                        &lt;endpoint address="net.msmq://localhost/private/SampleMSMQ" binding="netMsmqBinding" 

                                contract="MessageSvc.MSMQ.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" /&gt; 

                &lt;/client&gt; 

运行结果:

客户端调用时,如果没有启动服务端,那么消息会进入到消息队列中。等到服务端启动后,会执行消息队列中的所有消息。

OK

<a href="http://down.51cto.com/data/100781" target="_blank">[源码下载]</a>

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