基于MSMQ的WCF SOA中间件建设
来源:互联网 发布:windows 2008 asp 编辑:程序博客网 时间:2024/04/28 13:51
在windows平台上,MSMQ是首选的消息传递中间件,它是一种高速、异步、可靠的通信机制,当我们在Internet上的两个应用需要交换信息时,使用这样的中间件可能是必须的。
WCF完全面向SOA,大大简化了以往风格迥异的多种分布式解决方案。下面以一个例子:【使用SOA架构,底层使用MSMQ作为消息传递基础设施。】
首先定义服务端和客户端赖以沟通的Contract,通常将这些Contact定义在一个单独的dll中,如此可被服务端和客户端引用。我们假设一个简单的Contract,即一个接口ICalculate:
[ServiceContract]
public interface ICalculate
{
[OperationContract(IsOneWay=true)]
void DealOrder(string orderID);
}
例子中,我们将ICalculate定义在WcfLib.dll中。public interface ICalculate
{
[OperationContract(IsOneWay=true)]
void DealOrder(string orderID);
}
服务端需要实现ICalculate接口:
publicclass Calculator : ICalculate
{
publicvoid DealOrder(string orderID)
{
Program.FileLogger.Log(orderID);
}
}
接下来,服务端就可以以MSMQ的方式发布该服务了,这个可以在配置文件App.Config中进行配置:{
publicvoid DealOrder(string orderID)
{
Program.FileLogger.Log(orderID);
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfTest.Calculator">
<endpointaddress="net.msmq://localhost/private/WcfTest"
binding="netMsmqBinding"bindingConfiguration="msmq"
contract="WcfLib.ICalculate"/>
</service>
</services>
<bindings>
<netMsmqBinding>
<binding name="msmq">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
配置中红色部分标志了WCF的“ABC”,address表明了将使用本地的名为WcfTest的专用队列。请注意,binding配置后有一个bindingConfiguration,说明这个binding需要更高级的配置,相应的配置段在bindings Segment中,由于示例中使用的消息队列没有使用域模式,所以security mode 设为None,该配置会将MsmqAuthenticationMode属性设置为MsmqAuthenticationMode.None。另外,配置中显示的WcfTest专用队列需要被设置为“事务性”,在创建队列的时候可以选择此属性。<configuration>
<system.serviceModel>
<services>
<service name="WcfTest.Calculator">
<endpointaddress="net.msmq://localhost/private/WcfTest"
binding="netMsmqBinding"bindingConfiguration="msmq"
contract="WcfLib.ICalculate"/>
</service>
</services>
<bindings>
<netMsmqBinding>
<binding name="msmq">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
配置完成后,我们可以启动该服务了:
ServiceHost serviceHost =new ServiceHost(typeof(Calculator));
serviceHost.Open();
serviceHost.Open();
再来看客户端,非常简单,首先在App.Config中设置“ABC”(与服务端一致):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorClient"
address="net.msmq://localhost/private/WcfTest"
binding="netMsmqBinding"bindingConfiguration="msmq"
contract="WcfLib.ICalculate">
</endpoint>
</client>
<bindings>
<netMsmqBinding>
<binding name="msmq">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorClient"
address="net.msmq://localhost/private/WcfTest"
binding="netMsmqBinding"bindingConfiguration="msmq"
contract="WcfLib.ICalculate">
</endpoint>
</client>
<bindings>
<netMsmqBinding>
<binding name="msmq">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
在添加了对WcfLib.dll的引用后,接下来就可以调用服务了:
ChannelFactory<WcfLib.ICalculate> channelFactory=new ChannelFactory<ICalculate>("CalculatorClient");
ICalculate calculate = channelFactory.CreateChannel();
calculate.DealOrder(this.textBox1.Text);
ICalculate calculate = channelFactory.CreateChannel();
calculate.DealOrder(this.textBox1.Text);
使用MSMQ作为消息传递基础设施后,有这样一个好处,当Internet不可用或者服务端没有启动时,客户端仍然可以调用DealOrder方法将消息发送,当然,消息会暂存在队列中,等网络恢复或服务端启动后,这些队列中的消息将会被处理。
- 基于MSMQ的WCF SOA中间件建设
- 基于WCF MSMQ 的企业应用解决方案
- 基于WCF MSMQ 的企业应用解决方案
- 基于MSMQ使用WCF
- 基于MSMQ使用WCF
- wcf的 msmq
- 基于SOA的RFID中间件集成应用
- 基于WCF的SOA架构项目实战
- 基于WCF的PDA系统框架【SOA】
- 一种基于SOA的应用集成中间件体系架构
- WCF--MSMQ
- [原创]我的WCF之旅(13):创建基于MSMQ的Responsive Service
- 我的WCF之旅(13):创建基于MSMQ的Responsive Service
- 我的WCF之旅(13):创建基于MSMQ的Responsive Service
- 我的WCF之旅(13):创建基于MSMQ的Responsive Service
- 我的基于WCF的SOA架构项目实战
- MSMQ消息队列与WCF MSMQ的认识与使用
- 冷静面对SOA的中间件
- android源码解析--MessageQueue
- Android修改源代码控制永不锁屏
- 使用阿里云云服务器一年多的感受
- SQLite数据库操作
- 批处理:如何判断本地的某端口是否开放
- 基于MSMQ的WCF SOA中间件建设
- C#--,设计一个windows应用程序,实现不同运算。
- 设计精巧的openERP
- Linux下安装、启动MySQL
- C++实现的完整伸展树程序
- UBOOT LCD驱动流程 (
- memcached源码阅读笔记(一)
- 千品团购的一次糟糕体验
- Android AsyncTask异步处理