在WCF中使用消息隊列MSMQ

在WCF中使用消息隊列MSMQ windows

在windows平臺上,MSMQ是首選的消息傳遞中間件,它是一種高速、異步、可靠的通訊機制,當咱們在Internet上的兩個應用須要交換信息時,使用這樣的中間件多是必須的。網絡

    構建企業級可靠的、異步的、消息應用方案,方案的設計目標是在Client/Server端創建可靠的、異步的通訊。系統採用MSMQ做爲傳輸機制,由於MSMQ支持可靠的隊列通訊。MSMQ部署在三方Server上(通常集羣部署,避免單點故障)。Client端應用程序使用WCF的NetMsmqBingding來發送消息到MSMQ Server的私有隊列。Service 服務程序將部署在IIS 7.0,並採用Windows Activation Services(WAS)來監聽消息隊列上的新消息。經過SMSvnHost.exe – Windows 服務程序來實現監聽,當有消息到達時,它負責在IIS Worker process激活service服務,而後service服務將處理消息。總體的架構以下所示:架構

 

   WCF徹底面向SOA,大大簡化了以往風格迥異的多種分佈式解決方案。本事示例在WCF中使用MSMQ的方法。下面以一個例子說明。異步

   首先定義服務端和客戶端賴以溝通的Contract,一般將這些Contact定義在一個單獨的dll中,如此可被服務端和客戶端引用。咱們假設一個簡單的Contract,即一個接口ILoginService:分佈式

 

C#代碼this

  1. [ServiceContract]   
  2.  public interface ILoginService   
  3.  {   
  4.      ///<summary>  
  5.      ///SetHello  
  6.      ///</summary>  
  7.      ///<returns></returns>  
  8.      [OperationContract(IsOneWay = true)]   
  9.      void SetHello(string pStr);   

10. }       url

  1. 11.    

  

   服務端須要實現ILoginService接口:設計

   

C#代碼xml

  1. [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall)]   
  2.     public class LoginService:ILoginService   
  3.     {   
  4.         void ILoginService.SetHello(string pStr)   
  5.         {   
  6.             string bb = pStr;   
  7.         }   
  8.     }    

 

 接下來,服務端就能夠以MSMQ的方式發佈該服務了,這個能夠在配置文件App.Config中進行配置:中間件

 

XML/HTML代碼

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service name="WCFDemo.LoginService">  
  6.         <endpoint address="net.msmq://localhost/private/WcfTest"  
  7.                       binding="netMsmqBinding" bindingConfiguration="msmq"  
  8.                       contract="WCFDemo.ILoginService"/>  
  9.       </service>  
  10. 10.     </services>  
  11. 11.   
  12. 12.     <bindings>  
  13. 13.       <netMsmqBinding>  
  14. 14.         <binding name="msmq">  
  15. 15.           <security mode ="None"/>  
  16. 16.         </binding>  
  17. 17.       </netMsmqBinding>  
  18. 18.     </bindings>  
  19. 19.   
  20. 20.   </system.serviceModel>  

21. </configuration>  

 

   配置中,address代表了將使用本地的名爲WcfTest的專用隊列。請注意,binding配置後有一個bindingConfiguration,說明這個binding須要更高級的配置,相應的配置段在bindings Segment中,因爲示例中使用的消息隊列沒有使用域模式,因此security mode 設爲None,該配置會將MsmqAuthenticationMode屬性設置爲MsmqAuthenticationMode.None。另外,配置中顯示的WcfTest專用隊列須要被設置爲「事務性」,在建立隊列的時候能夠選擇此屬性。

   配置完成後,咱們能夠啓動該服務了:

C#代碼

  1. new ServiceHost(typeof(WCFDemo.LoginService)).Open();     

 

   再來看客戶端,很是簡單,首先在App.Config中設置「ABC」(與服務端一致):

XML/HTML代碼

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <client>  
  5.       <endpoint name="HelloClient"  
  6.                 address="net.msmq://localhost/private/WcfTest"  
  7.                 binding="netMsmqBinding" bindingConfiguration="msmq"  
  8.                 contract="WCFDemo.ILoginService">  
  9.       </endpoint>  
  10. 10.     </client>  
  11. 11.   
  12. 12.     <bindings>  
  13. 13.       <netMsmqBinding>  
  14. 14.         <binding name="msmq">  
  15. 15.           <security mode ="None"/>  
  16. 16.         </binding>  
  17. 17.       </netMsmqBinding>  
  18. 18.     </bindings>  
  19. 19.   </system.serviceModel>  

20. </configuration>  

 

   在添加了對WCFDemo.dll的引用後,接下來就能夠調用服務了:

C#代碼

  1. ChannelFactory<WCFDemo.ILoginService> channelFactory = new ChannelFactory<ILoginService>("HelloClient");   
  2. ILoginService calculate = channelFactory.CreateChannel();   
  3. calculate.SetHello(this.textBox1.Text);  

 

 

   使用MSMQ做爲消息傳遞基礎設施後,有這樣一個好處,當Internet不可用或者服務端沒有啓動時,客戶端仍然能夠調用SetHello方法將消息發送,固然,消息會暫存在隊列中,等網絡恢復或服務端啓動後,這些隊列中的消息將會被處理。 

http://wenku.baidu.com/link?url=8ZvWBf2QA4E3FURrKW-qb7XLB1HxB9AkATqv5VCxPotNzjWKh75klBC7SEb-82z3evUezq1n8SOfnIE1BOW2SQ1GSHZweebpJbkoeg-2UUS

相關文章
相關標籤/搜索