[索引页]
[源码下载]


化零为整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
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)using System;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Collections.Generic;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Linq;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Text;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.ServiceModel;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
namespace WCF.ServiceLib.Message

MSMQ.cs
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)using System;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Collections.Generic;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Linq;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Text;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.ServiceModel;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
namespace WCF.ServiceLib.Message


2、宿主
MSMQ.cs
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)// 队列名
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
// 只能使用.\private$\YourPrivateMSMQName来访问本机的私有MSMQ队列
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
string queueName = @".\private$\SampleMSMQ";
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
// 没有queueName队列,则创建queueName队列
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
if (!System.Messaging.MessageQueue.Exists(queueName))

App.config
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)<?xml version="1.0" encoding="utf-8" ?>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
<configuration>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)    
<system.serviceModel>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
<services>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--name - 提供服务的类名-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--behaviorConfiguration - 指定相关的行为配置-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<service name="WCF.ServiceLib.Message.MSMQ" behaviorConfiguration="MessageBehavior">
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<!--address - 服务地址-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<!--binding - 通信方式-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<!--contract - 服务契约-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<!--bindingConfiguration - 指定相关的绑定配置-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<endpoint address="" binding="netMsmqBinding" contract="WCF.ServiceLib.Message.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<host>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<baseAddresses>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<add baseAddress="http://localhost:12345/Message/MSMQ"/>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<add baseAddress="net.msmq://localhost/private/SampleMSMQ"/>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
</baseAddresses>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
</host>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
</service>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
</services>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
<behaviors>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<serviceBehaviors>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<behavior name="MessageBehavior">
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<serviceMetadata httpGetEnabled="true" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<serviceDebug includeExceptionDetailInFaults="true"/>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
</behavior>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
</serviceBehaviors>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
</behaviors>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
<bindings>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<netMsmqBinding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<binding name="MSMQBindingConfiguration">
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<security>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.None - 不使用任何安全性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.None - 只做身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<message clientCredentialType="UserName" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
</security>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
</binding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
</netMsmqBinding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
</bindings>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)    
</system.serviceModel>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
</configuration>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)


3、客户端
MSMQ.cs
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)using System;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Collections.Generic;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Linq;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Text;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.Windows.Forms;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
using System.ServiceModel;
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
namespace Client2.Message

App.config
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)<?xml version="1.0" encoding="utf-8" ?>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
<configuration>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)    
<system.serviceModel>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
<client>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--address - 服务地址-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--binding - 通信方式-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--contract - 服务契约-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<!--bindingConfiguration - 指定相关的绑定配置-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<endpoint address="net.msmq://localhost/private/SampleMSMQ" binding="netMsmqBinding"
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                contract
="MessageSvc.MSMQ.IMSMQ" bindingConfiguration="MSMQBindingConfiguration" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
</client>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
<bindings>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
<netMsmqBinding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
<binding name="MSMQBindingConfiguration">
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
<security>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.None - 不使用任何安全性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.None - 只做身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName -->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证-->
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                        
<message clientCredentialType="UserName" />
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                    
</security>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)                
</binding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)            
</netMsmqBinding>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)        
</bindings>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)    
</system.serviceModel>
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
</configuration>


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


OK
[源码下载]

相关文章:

  • 2021-04-01
  • 2021-09-06
  • 2021-12-07
  • 2021-07-20
  • 2022-01-08
  • 2021-11-14
  • 2021-07-04
  • 2022-12-23
猜你喜欢
  • 2021-09-14
  • 2021-12-05
  • 2021-05-01
  • 2021-09-25
  • 2021-07-11
相关资源
相似解决方案