MSMQ使用WCF正确实现技巧讲解

在了解了WCF之后,大家应该会被他强大的功能,突出的优势所深深吸引。它能够为我们轻松的打造一个跨平台的安全性极强的解决方案。在这里我们会为大家介绍一下MSMQ使用WCF的实现方法。#t#

在windows平台上,MSMQ是***的消息传递中间件,它是一种高速、异步、可靠的通信机制,当我们在Internet上的两个应用需要交换信息时,使用这样的中间件可能是必须的。

WCF完全面向SOA,大大简化了以往风格迥异的多种分布式解决方案。刚好,最近的一个项目需要使用SOA架构,而底层需要使用MSMQ作为消息传递基础设施,所以这两天研究了一下,MSMQ使用WCF的方法。下面以一个例子说明。

首先定义服务端和客户端赖以沟通的Contract,通常将这些Contact定义在一个单独的dll中,如此可被服务端和客户端引用。我们假设一个简单的Contract,即一个接口ICalculate:

 
 
 
  1. [ServiceContract] 
  2. [ServiceContract] 
  3. public interface ICalculate 
  4. [OperationContract(IsOneWay=true)] 
  5. void DealOrder(string orderID); 
  6. }

例子中,我们将ICalculate定义在WcfLib.dll中。

服务端需要实现ICalculate接口:

 
 
 
  1. public class Calculator : ICalculate 
  2. public void DealOrder(string orderID) 
  3. Program.FileLogger.Log(orderID); 
  4. }

接下来,服务端就可以以MSMQ的方式发布该服务了,这个可以在配置文件App.Config中进行配置:

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

配置中红色部分标志了WCF的“ABC”,address表明了将使用本地的名为WcfTest的专用队列。请注意,binding配置后有一个bindingConfiguration,说明这个binding需要更高级的配置,相应的配置段在bindings Segment中,由于示例中使用的消息队列没有使用域模式,所以security mode 设为None,该配置会将MsmqAuthenticationMode属性设置为MsmqAuthenticationMode.None。另外,配置中显示的WcfTest专用队列需要被设置为“事务性”,在创建队列的时候可以选择此属性。

配置完成后,我们可以启动MSMQ使用WCF的服务了:

 
 
 
  1. ServiceHost serviceHost = new ServiceHost(typeof(Calculator)); 
  2. serviceHost.Open(); 

再来看客户端,非常简单,首先在App.Config中设置“ABC”(与服务端一致):

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

在添加了对WcfLib.dll的引用后,接下来就可以调用服务了:

 
 
 
  1. ChannelFactory< WcfLib.ICalculate> channelFactory =
     new ChannelFactory< ICalculate>("CalculatorClient"); 
  2. ICalculate calculate = channelFactory.CreateChannel(); 
  3. calculate.DealOrder(this.textBox1.Text); 

MSMQ使用WCF作为消息传递基础设施后,有这样一个好处,当Internet不可用或者服务端没有启动时,客户端仍然可以调用DealOrder方法将消息发送,当然,消息会暂存在队列中,等网络恢复或服务端启动后,这些队列中的消息将会被处理。

网站名称:MSMQ使用WCF正确实现技巧讲解
文章路径:http://www.shufengxianlan.com/qtweb/news31/337431.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联