<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      我的WCF之旅(12):使用MSMQ進行Reliable Messaging

      一、為什么要使用MSMQ

      在一個分布式的環(huán)境中,我們往往需要根據(jù)具體的情況采用不同的方式進行數(shù)據(jù)的傳輸。比如在一個Intranet內(nèi),我們一般通過TCP進行高效的數(shù)據(jù)通信;而在一個Internet的環(huán)境中,我們則通常使用Http進行跨平臺的數(shù)據(jù)交換。而這些通信方式具有一個顯著的特點,那就是他們是基于Connection的,也就是說,交互雙方在進行通信的時候必須保證有一個可用的Connection存在于他們之間。而在某些時候,比如那些使用撥號連接的用戶、以及使用便攜式計算機的用戶,我們不能保證在他們和需要訪問的Server之間有一個的可靠的連接,在這種情況下,基于Messaging Queue的連接就顯得尤為重要了。我們今天就來談談在WCF中如何使用MSMQ。

      MSMQ不僅僅是作為支持客戶端連接工具而存在,合理的使用MSMQ可以在很大程度上提升系統(tǒng)的Performance和Scalability。我們先來看看MSMQ能給我們帶來怎樣的好處:

      1.MSMQ是基于Disconnection

      MSMQ通過Message Queue進行通信,這種通信方式為離線工作成為了可能。比如在介紹MSMQ時都會提到的Order Delivery的例子:在一個基于B2C的系統(tǒng)中,訂單從各種各樣的客戶傳來,由于 客戶的各異性,不能保證每個客戶在每時每刻都和用于接收訂單的Server保持一個可靠的連接,我們有時候甚至允許客戶即使在離線的情況下也可以遞交訂單(雖然訂單不能發(fā)送到訂單的接收方,但是我們可以通過某種機制保證先在本地保存該訂單,一旦連接建立,則馬上向接收方遞交訂單),而MSMQ則有效地提供了這樣的機制:Server端建立一個Message Queue來接收來個客戶的訂單,客戶端通過向該Message Queue發(fā)送承載了訂單數(shù)據(jù)的Message實現(xiàn)訂單的遞交。如果在客戶離線的情況下,他仍然可以通過客戶端程序進行訂單遞交的操作,存儲著訂單數(shù)據(jù)的Message會被暫時保存在本地的Message Queue中,一旦客戶聯(lián)機,MSMQ將Message從中取出,發(fā)送到真正的接收方,而這個動作對于用戶的透明的。

      2.MSMQ天生是One-way、異步的

      在MSMQ中,Message始終以One-way的方式進行發(fā)送,所以MSMQ具有天生的異步特性。所以MSMQ使用于那些對于用戶的請求,Server端無需立即響應的場景。也就是說Server對數(shù)據(jù)的處理無需和Client的數(shù)據(jù)的發(fā)送進行同步,它可以獨自地按照自己的Schedule進行工作。這可以避免峰值負載。比如Server端可以在一個相對低負載的時段(比如深夜)來對接收到的Order進行批處理,而無需一天24小時一直進行Order的監(jiān)聽、接收和處理。

      3.MSMQ能夠提供高質(zhì)量的Reliable Messaging

      我們知道,在一般的情況下,如果Client端以異步的方式對Service進行調(diào)用就意味著:Client無法獲知Message是否成功抵達Service端;也不會獲得Service端執(zhí)行的結(jié)果和出錯信息。但是我們?nèi)匀徽fMSMQ為我們提供了可靠的傳輸(Reliable Messaging),這主要是因為MSMQ為我們提供一些列Reliable Messaging的機制:

      • 超時機制(Timeout):可以設置發(fā)送和接收的時間,超出該時間則被認為操作失敗。
      • 確認機制(Acknowledgement):當Message成功抵達Destination Queue,或者被成功接收,向發(fā)送端發(fā)送一個Acknowledgement message用以確認操作的狀態(tài)。
      • 日志機制(Journaling):當Message被發(fā)送或接收后,被Copy一份存放在Journal Queue中。

      此外,MSMQ還提供了死信隊列(Dead letter Queue)用以保存發(fā)送失敗的message。這一切保證了保證了Reliable Messaging。

      二、 MSMQ在WCF的運用

      在WCF中,MSMQ提供的數(shù)據(jù)傳輸功能被封裝在一個Binding中,提供WCF Endpoint之間、以及Endpoint和現(xiàn)有的基于MSMQ的Application進行通信的實現(xiàn)。為此WCF為我們提供了兩種不同的built-in binding:

      • NetMsmqBinding:從提供的功能和使用 方式上看,NetMsmqBinding和一般使用的binding,比如basicHttpBinding,netTcpBinding沒有什么區(qū)別:在兩個Endpoint之間實現(xiàn)了數(shù)據(jù)的通信,所不同的是,它提供的是基于MSMQ的Reliable Messaging。從變成模式上看,和一般的binding完全一樣。
      • MsmqIntegrationBinding:從命名上我們可以看出,MsmqIntegrationBinding主要用于需要將我們的WCF Application和現(xiàn)有的基于MSMQ的Application集成的情況。MsmqIntegrationBinding實現(xiàn)了WCF Endpoint和某個Message Queue進行數(shù)據(jù)的通信,具體來說,就是實現(xiàn)了單一的向某個Message Queue 發(fā)送Message,和從某個Message Queue中接收Message的功能。從編程模式上看,也有所不同,比如Operation只接收一個MsmqMessage<T>的參數(shù)。

      這是Client和Service通信的圖示:

      三、MSMQ和Transaction

      MSMQ提供對Transaction的支持。在一般的情況下,MSMQ通過Message Queue Transaction實現(xiàn)對Transaction的原生的支持,借助Message Queue Transaction,可以把基于一個或多個Message Queue的相關操作納入同一個Transaction中。

      Message Queue Transaction僅僅限于基于Message Queue的操作,倘若操作涉及到另外一些資源,比如SQL Server, 則可以使用基于DTC的分布式Transaction

      對于WCF中MSMQ,由于Client和Service的相對獨立(可能Client發(fā)送Message到Service處理Message會相隔很長一段時間),所以Client和Service的操作只能納入不同的Transaction中,如下圖。

      四、Sample1:NetMsmqBinding

      我們首先做一個基于NetMsmqBinding Sample,實現(xiàn)的功能就是我們開篇所提出的Order Delivery。我們說過,NetMsmqBinding和一般的binding在實現(xiàn)的功能和變成模式上完全一樣。下面是我們熟悉的4層結(jié)構(gòu):


      1.Contract

      DataContract:Order & OrderItem

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Runtime.Serialization;

      namespace Artech.QueuedService.Contract
      {
          [DataContract]
          [KnownType(
      typeof(OrderItem))]
          
      public class Order
          
      {
              
      Private Fields

              
      Constructors

              
      Public Properties

              
      Public Methods
          }

      }


      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Runtime.Serialization;

      namespace Artech.QueuedService.Contract
      {
          [DataContract]
          
      public class OrderItem
          
      {
              
      Private Fields

              
      Constructors

              
      Public Properties
          }

      }

      ServiceContract: IOrderProcessor

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.ServiceModel;

      namespace Artech.QueuedService.Contract
      {
          [ServiceContract] 
          [ServiceKnownType(
      typeof(Order))]
          
      public interface IOrderProcessor
          
      {
              [OperationContract(IsOneWay 
      = true)]
              
      void Submit(Order order);
          }

      }

      2.Service:IOrderProcessor

      using System;
      using System.Collections.Generic;
      using System.Text;
      using Artech.QueuedService.Contract;
      using System.ServiceModel;

      namespace Artech.QueuedService.Service
      {
          
      public class OrderProcessorService:IOrderProcessor
          
      {
              
      ISubmitOrder Members
          }

      }

      using System;
      using System.Collections.Generic;
      using System.Text;
      using Artech.QueuedService.Contract;

      namespace Artech.QueuedService.Service
      {
          
      public static class Orders
          
      {
              
      private static IDictionary<Guid, Order> _orderList = new Dictionary<Guid, Order>();

              
      public static void Add(Order order)
              
      {
                  _orderList.Add(order.OrderNo, order);
              }


              
      public static Order GetOrder(Guid orderNo)
              
      {
                  
      return _orderList[orderNo];
              }

          }

      }

      3.Hosting

      Configuration

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          
      <system.serviceModel>
              
      <bindings>
                  
      <netMsmqBinding>
                      
      <binding name="msmqBinding">
                          
      <security>
                              
      <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
                              
      <message clientCredentialType="None" />
                          
      </security>
                      
      </binding>
                  
      </netMsmqBinding>
              
      </bindings>
              
      <services>
                  
      <service name="Artech.QueuedService.Service. OrderProcessorService">
                      
      <endpoint address="net.msmq://localhost/private/orders" binding="netMsmqBinding"
                          bindingConfiguration
      ="msmqBinding" contract="Artech.QueuedService.Contract.IOrderProcessor" />
                  
      </service>
              
      </services>
          
      </system.serviceModel>
      </configuration>

      在默認的情況下,netMsmqBinding 的msmqAuthenticationModeWindowsDomain,由于基于WindowsDomain必須安裝AD,利于在本機模擬,我把msmqAuthenticationMode改為None,相應的ProtectionLevel和clientCredentialType改為None。

      Program:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Messaging;
      using System.ServiceModel;
      using Artech.QueuedService.Service;

      namespace Artech.QueuedService.Hosting
      {
          
      class Program
          
      {
              
      static void Main(string[] args)
              
      {
                  
      string path = @".\private$\orders";
                  
      if(!MessageQueue.Exists(path))
                  
      {
                      MessageQueue.Create(path,
      true);
                  }


                  
      using (ServiceHost host = new ServiceHost(typeof(OrderProcessorService)))
                  
      {
                      host.Opened 
      += delegate
                      
      {
                          Console.WriteLine(
      "Service has begun to listen\n\n");
                      }
      ;

                      host.Open();

                      Console.Read();
                  }

              }

          }

      }

      在Host Service之前,通過MessageQueue.Create創(chuàng)建一個Message Queue,第二個參數(shù)為表明Queue是否支持Transaction的indicator,這里支持Transaction。

      4.Client:

      Configuration

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          
      <system.serviceModel>
              
      <bindings>
                  
      <netMsmqBinding>
                      
      <binding name="msmqBinding">
                          
      <security>
                              
      <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
                              
      <message clientCredentialType="None" />
                          
      </security>
                      
      </binding>
                  
      </netMsmqBinding>
              
      </bindings>
              
      <client>
                  
      <endpoint address="net.msmq://localhost/private/orders" binding="netMsmqBinding"
                      bindingConfiguration
      ="msmqBinding" contract="Artech.QueuedService.Contract.IOrderProcessor"
                      name
      ="defaultEndpoint" />
              
      </client>
          
      </system.serviceModel>
      </configuration>

      Program

      using System;
      using System.Collections.Generic;
      using System.Text;
      using Artech.QueuedService.Contract;
      using System.ServiceModel;
      using System.Transactions;

      namespace Artech.QueuedService.Client
      {
          
      class Program
          
      {
              
      static void Main(string[] args)
              
      {
                  ChannelFactory
      <IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>("defaultEndpoint");
                  IOrderProcessor channel 
      = channelFactory.CreateChannel();

                  Order order 
      = new Order(Guid.NewGuid(),DateTime.Today,Guid.NewGuid(),"A Company");
                  order.OrderItems.Add(
      new OrderItem(Guid.NewGuid(),"PC",5000,20));
                  order.OrderItems.Add(
      new OrderItem(Guid.NewGuid(),"Printer",7000,2));

                  Console.WriteLine(
      "Submit order to server");
                  
      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                  

                       channel.Submit(order);
                       scope.Complete();
                  }
             
                  Console.Read();
              }

          }

      }

      先后運行Host和Client,Host端有下面的輸出:


      WCF相關內(nèi)容:
      [原創(chuàng)]我的WCF之旅(1):創(chuàng)建一個簡單的WCF程序
      [原創(chuàng)]我的WCF之旅(2):Endpoint Overview
      [原創(chuàng)]我的WCF之旅(3):在WCF中實現(xiàn)雙向通信(Bi-directional Communication)
      [原創(chuàng)]我的WCF之旅(4):WCF中的序列化(Serialization)- Part I
      [原創(chuàng)]我的WCF之旅(4):WCF中的序列化(Serialization)- Part II
      [原創(chuàng)]我的WCF之旅(5):Service Contract中的重載(Overloading)
      [原創(chuàng)]我的WCF之旅(6):在Winform Application中調(diào)用Duplex Service出現(xiàn)TimeoutException的原因和解決方案
      [原創(chuàng)]我的WCF之旅(7):面向服務架構(gòu)(SOA)和面向?qū)ο缶幊蹋∣OP)的結(jié)合——如何實現(xiàn)Service Contract的繼承
      [原創(chuàng)]我的WCF之旅(8):WCF中的Session和Instancing Management
      [原創(chuàng)]我的WCF之旅(9):如何在WCF中使用tcpTrace來進行Soap Trace
      [原創(chuàng)]我的WCF之旅(10): 如何在WCF進行Exception Handling
      [原創(chuàng)]我的WCF之旅(11):再談WCF的雙向通訊-基于Http的雙向通訊 V.S. 基于TCP的雙向通訊

      [原創(chuàng)]我的WCF之旅(12):使用MSMQ進行Reliable Messaging
      [原創(chuàng)]我的WCF之旅(13):創(chuàng)建基于MSMQ的Responsive Service
      posted @ 2007-06-29 00:57  Artech  閱讀(21809)  評論(74)    收藏  舉報
      主站蜘蛛池模板: 久久中文字幕av第二页| 麻豆国产传媒精品视频| 亚洲一二区制服无码中字| 日本毛茸茸的丰满熟妇| 色噜噜狠狠成人综合| 国产仑乱无码内谢| 欧美日韩国产va在线观看免费| 人妻少妇精品专区性色av| 亚洲 欧美 中文 日韩aⅴ| 欧美成人精品手机在线| 久久精品日日躁夜夜躁| 2021亚洲va在线va天堂va国产 | 天堂а√在线地址中文在线| 亚洲av日韩av综合在线观看| 精品 日韩 国产 欧美 视频| 久久精品国产亚洲av麻豆小说 | 午夜av福利一区二区三区| 亚洲中文日韩一区二区三区| 狠狠综合久久综合88亚洲| 又污又黄又无遮挡的网站| 亚洲国产成人综合精品| 亚洲综合色成在线播放| 国产短视频精品一区二区| 一区二区精品久久蜜精品| 欧美性大战xxxxx久久久| 国产成人啪精品午夜网站| 高清在线一区二区三区视频| 久久天天躁狠狠躁夜夜躁2o2o| 国产对白老熟女正在播放| 亚洲熟少妇一区二区三区| 秋霞人妻无码中文字幕| 一区二区三区不卡国产| 国产精品毛片一区二区| 少妇人妻偷人精品视蜜桃| 保定市| 又大又粗又硬又爽黄毛少妇| 丁香五月婷激情综合第九色| 马边| 久久国内精品一国内精品| 国内少妇偷人精品免费| 国产另类ts人妖一区二区|