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

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


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


作者: webabcd


介绍
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
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         /// 演示MSMQ的接口 
InBlock.gif         /// </summary> 
InBlock.gif         /// <remarks> 
InBlock.gif        /// DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求 
InBlock.gif        /// QueuedDeliveryRequirements - 指定服务的绑定是否必须支持排队协定 
InBlock.gif        /// QueuedDeliveryRequirementsMode.Allowed - 允许排队传送 
InBlock.gif        /// QueuedDeliveryRequirementsMode.Required - 要求排队传送 
InBlock.gif        /// QueuedDeliveryRequirementsMode.NotAllowed - 不允许排队传送 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceContract] 
InBlock.gif        [DeliveryRequirements(QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Required)] 
InBlock.gif         public  interface IMSMQ 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 将字符串写入文本文件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="str">需要写入文本文件的字符串</param> 
InBlock.gif                 /// <remarks> 
InBlock.gif                /// 如果要使用 MSMQ 的话,则必须配置IsOneWay = true 
InBlock.gif                /// </remarks> 

InBlock.gif                [OperationContract(IsOneWay =  true)] 
InBlock.gif                 void Write( string str); 
InBlock.gif        } 
InBlock.gif}
 
MSMQ.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         /// 演示MSMQ的类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class MSMQ : IMSMQ 
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_MSMQ.txt"true); 
InBlock.gif                        sw.Write(str); 
InBlock.gif                        sw.WriteLine(); 
InBlock.gif                        sw.Close(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
2、宿主
MSMQ.cs
InBlock.gif // 队列名 
InBlock.gif // 只能使用.\private$\YourPrivateMSMQName来访问本机的私有MSMQ队列 
InBlock.gif string queueName =  @".\private$\SampleMSMQ"
InBlock.gif 
InBlock.gif // 没有queueName队列,则创建queueName队列 
InBlock.gif if (!System.Messaging.MessageQueue.Exists(queueName)) 
InBlock.gif
InBlock.gif         // 第二个参数为是否创建事务性队列 
InBlock.gif        System.Messaging.MessageQueue.Create(queueName,  true); 
InBlock.gif
InBlock.gif 
InBlock.gif using (ServiceHost host =  new ServiceHost( typeof(WCF.ServiceLib.Message.MSMQ))) 
InBlock.gif
InBlock.gif        host.Open(); 
InBlock.gif 
InBlock.gif        Console.WriteLine( "服务已启动(WCF.ServiceLib.Message.MSMQ)"); 
InBlock.gif        Console.WriteLine( "按<ENTER>停止服务"); 
InBlock.gif        Console.ReadLine(); 
InBlock.gif 
InBlock.gif}
 
App.config
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
        <system.serviceModel> 
                <services> 
                        <!--name - 提供服务的类名--> 
                        <!--behaviorConfiguration - 指定相关的行为配置--> 
                        <service name="WCF.ServiceLib.Message.MSMQ" behaviorConfiguration="MessageBehavior"> 
                                <!--address - 服务地址--> 
                                <!--binding - 通信方式--> 
                                <!--contract - 服务契约--> 
                                <!--bindingConfiguration - 指定相关的绑定配置--> 
                                <endpoint address="" binding="netMsmqBinding" contract="WCF.ServiceLib.Message.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" /> 
                                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
                                <host> 
                                        <baseAddresses> 
                                                <add baseAddress="http://localhost:12345/Message/MSMQ"/> 
                                                <add baseAddress="net.msmq://localhost/private/SampleMSMQ"/> 
                                        </baseAddresses> 
                                </host> 
                        </service> 
                </services> 
                <behaviors> 
                        <serviceBehaviors> 
                                <behavior name="MessageBehavior"> 
                                        <!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false--> 
                                        <serviceMetadata httpGetEnabled="true" /> 
                                        <serviceDebug includeExceptionDetailInFaults="true"/> 
                                </behavior> 
                        </serviceBehaviors> 
                </behaviors> 
                <bindings> 
                        <netMsmqBinding> 
                                <binding name="MSMQBindingConfiguration"> 
                                        <security> 
                                                <!--msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain --> 
                                                <!--MsmqAuthenticationMode.None - 不使用任何安全性--> 
                                                <!--MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域--> 
                                                <!--MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中--> 

                                                <!--msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign --> 
                                                <!--ProtectionLevel.None - 只做身份验证--> 
                                                <!--ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性--> 
                                                <!--ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性--> 
                                                <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" /> 

                                                <!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName --> 
                                                <!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证--> 
                                                <!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证--> 
                                                <message clientCredentialType="UserName" /> 
                                        </security> 
                                </binding> 
                        </netMsmqBinding> 
                </bindings> 
        </system.serviceModel> 
</configuration>
 
 
3、客户端
MSMQ.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.Windows.Forms; 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace Client2.Message 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 演示Message.MSMQ的类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class MSMQ 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 用于测试 MSMQ 的客户端 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="str">需要写入文本文件的字符串</param> 
InBlock.gif                 public  void HelloMSMQ( string str) 
InBlock.gif                { 
InBlock.gif                         using (var proxy =  new MessageSvc.MSMQ.MSMQClient()) 
InBlock.gif                        { 
InBlock.gif                                proxy.Write(str); 
InBlock.gif                        } 
InBlock.gif                }             
InBlock.gif        } 
InBlock.gif}
 
App.config
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
        <system.serviceModel> 
                <client> 
                        <!--address - 服务地址--> 
                        <!--binding - 通信方式--> 
                        <!--contract - 服务契约--> 
                        <!--bindingConfiguration - 指定相关的绑定配置--> 
                        <endpoint address="net.msmq://localhost/private/SampleMSMQ" binding="netMsmqBinding" 
                                contract="MessageSvc.MSMQ.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" /> 
                </client> 
                <bindings> 
                        <netMsmqBinding> 
                                <binding name="MSMQBindingConfiguration"> 
                                        <security> 
                                                <!--msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain --> 
                                                <!--MsmqAuthenticationMode.None - 不使用任何安全性--> 
                                                <!--MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域--> 
                                                <!--MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中--> 

                                                <!--msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign --> 
                                                <!--ProtectionLevel.None - 只做身份验证--> 
                                                <!--ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性--> 
                                                <!--ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性--> 
                                                <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" /> 

                                                <!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName --> 
                                                <!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证--> 
                                                <!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证--> 
                                                <message clientCredentialType="UserName" /> 
                                        </security> 
                                </binding> 
                        </netMsmqBinding> 
                </bindings> 
        </system.serviceModel> 
</configuration>
 
 
运行结果:
客户端调用时,如果没有启动服务端,那么消息会进入到消息队列中。等到服务端启动后,会执行消息队列中的所有消息。


OK
[源码下载]
 

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

相关文章
|
9月前
|
消息中间件 中间件 C#
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(二)
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(二)
|
9月前
|
消息中间件 存储 C#
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(一)
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(一)
|
消息中间件
消息队列 MQ——名称 含义 Message 消息
消息队列 MQ——名称 含义 Message 消息自制脑图
90 0
消息队列 MQ——名称 含义 Message 消息
|
安全 网络协议 网络安全
WCF安全3-Transport与Message安全模式
WCF安全3-Transport与Message安全模式
WCF安全3-Transport与Message安全模式
|
消息中间件 Java C++
开源项目推荐:多进程和多线程的高性能消息队列(无锁队列),lock-free queue
开源项目推荐:多进程和多线程的高性能消息队列(无锁队列),lock-free queue
909 0
|
消息中间件 C#
【c#】队列(Queue)和MSMQ(消息队列)的基础使用
原文:【c#】队列(Queue)和MSMQ(消息队列)的基础使用       首先我们知道队列是先进先出的机制,所以在处理并发是个不错的选择。然后就写两个队列的简单应用。 Queue 命名空间     命名空间:System.Collections,不在这里做过多的理论解释,这个东西非常的好理解。
1433 0
|
XML 网络架构 数据格式