zoukankan      html  css  js  c++  java
  • 在​W​C​F​中​使​用​消​息​队​列​M​S​M​Q

    在WCF中使用消息队列MSMQ 

    在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#代码

    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. }       

    1. 11.    

      

       服务端需要实现ILoginService接口:

       

    C#代码

    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

  • 相关阅读:
    App更新之dialog数字进度条
    Android app启动是出现白屏或者黑屏如何解决?
    Tensorflow报错:AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile'
    《Python深度学习》第三章阅读笔记
    在Ubuntu 18.04上配置CPU深度学习环境
    《Python深度学习》第二章阅读笔记
    《Python深度学习》第一章阅读笔记
    POJ 1118 Lining Up
    蓝桥杯-地宫取宝(动态规划)
    洛谷P2280[HNOI2003] 激光炸弹(二维前缀和)
  • 原文地址:https://www.cnblogs.com/Jessy/p/3648972.html
Copyright © 2011-2022 走看看