天天看點

化零為整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,如需轉載請自行聯系原作者