在WCF中使用消息队列MSMQ
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
在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
相关文章推荐
- 模拟Matlab显示图像的效果
- 大二的时候的一个小项目---实现四则运算的逻辑分析实现(小型编译器)
- poj1128 Frame Stacking
- stl-vector
- Asp.net—DropDownList与listbox的联动
- NiNetworkImageView下载图片,goole地图图片的获取
- 友善之臂 mini2440 root_qtopia 文件系统启动过程分析
- mysql命令
- jboss配置https
- msocache是什么文件夹
- MEMS麥克風具降噪、高效能、小尺寸優勢
- DetailsView的添加,修改,删除,查询
- Java语言基础(四)
- oracle面试题
- windows核心编程1-4笔记
- ZOJ Problem Set - 1847 The Trip
- Unity-WIKI 之 AllocationStats(内存分配)
- 在Hibernate中使用多表查询的一种实现
- HDU 2121 Ice_cream's world II 朱刘算法求无固定根最小树形图
- deque queue and priority_queue