您的位置:首页 > 其它

在​W​C​F​中​使​用​消​息​队​列​M​S​M​Q

2014-04-06 18:16 337 查看
在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#代码

[ServiceContract]

public interface ILoginService

{

///<summary>

///SetHello

///</summary>

///<returns></returns>

[OperationContract(IsOneWay = true)]

void SetHello(string pStr);

10. }

11.

服务端需要实现ILoginService接口:

C#代码

[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall)]

public class LoginService:ILoginService

{

void ILoginService.SetHello(string pStr)

{

string bb = pStr;

}

}

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

XML/HTML代码

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<services>

<service name="WCFDemo.LoginService">

<endpoint address="net.msmq://localhost/private/WcfTest"

binding="netMsmqBinding" bindingConfiguration="msmq"

contract="WCFDemo.ILoginService"/>

</service>

10. </services>

11.

12. <bindings>

13. <netMsmqBinding>

14. <binding name="msmq">

15. <security mode ="None"/>

16. </binding>

17. </netMsmqBinding>

18. </bindings>

19.

20. </system.serviceModel>

21. </configuration>

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

配置完成后,我们可以启动该服务了:

C#代码

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

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

XML/HTML代码

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<client>

<endpoint name="HelloClient"

address="net.msmq://localhost/private/WcfTest"

binding="netMsmqBinding" bindingConfiguration="msmq"

contract="WCFDemo.ILoginService">

</endpoint>

10. </client>

11.

12. <bindings>

13. <netMsmqBinding>

14. <binding name="msmq">

15. <security mode ="None"/>

16. </binding>

17. </netMsmqBinding>

18. </bindings>

19. </system.serviceModel>

20. </configuration>

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

C#代码

ChannelFactory<WCFDemo.ILoginService> channelFactory = new ChannelFactory<ILoginService>("HelloClient");

ILoginService calculate = channelFactory.CreateChannel();

calculate.SetHello(this.textBox1.Text);

使用MSMQ作为消息传递基础设施后,有这样一个好处,当Internet不可用或者服务端没有启动时,客户端仍然可以调用SetHello方法将消息发送,当然,消息会暂存在队列中,等网络恢复或服务端启动后,这些队列中的消息将会被处理。
http://wenku.baidu.com/link?url=8ZvWBf2QA4E3FURrKW-qb7XLB1HxB9AkATqv5VCxPotNzjWKh75klBC7SEb-82z3evUezq1n8SOfnIE1BOW2SQ1GSHZweebpJbkoeg-2UUS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: