基于MSMQ使用WCF

在windows平台上,MSMQ是首选的消息传递中间件,它是一种高速、异步、可靠的通信机制,当我们在Internet上的两个应用需要交换信息时,使用这样的中间件可能是必须的。
WCF完全面向SOA,大大简化了以往风格迥异的多种分布式解决方案。刚好,最近的一个项目需要使用SOA架构,而底层需要使用MSMQ作为消息传递基础设施,所以这两天研究了一下,在WCF中使用MSMQ的方法。下面以一个例子说明。
首先定义服务端和客户端赖以沟通的Contract,通常将这些Contact定义在一个单独的dll中,如此可被服务端和客户端引用。我们假设一个简单的Contract,即一个接口ICalculate:
[ServiceContract]
public interface ICalculate
{
[OperationContract(IsOneWay
= true )]
void DealOrder( string orderID);
}
例子中,我们将ICalculate定义在WcfLib.dll中。

服务端需要实现ICalculate接口:
public class Calculator:ICalculate
{

public void DealOrder( string orderID)
{
Program.FileLogger.Log(orderID);
}
}
接下来,服务端就可以以MSMQ的方式发布该服务了,这个可以在配置文件App.Config中进行配置:
<? xmlversion = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< system.serviceModel >
< services >
< servicename = " WcfTest.Calculator " >
< endpointaddress = " net.msmq://localhost/private/WcfTest "
binding
= " netMsmqBinding " bindingConfiguration = " msmq "
contract
= " WcfLib.ICalculate " />
</ service >
</ services >

< bindings >
< netMsmqBinding >
< bindingname = " msmq " >
<securitymode="None"/>
</ binding >
</ netMsmqBinding >
</ bindings >

</ system.serviceModel >
</ configuration >
配置中红色部分标志了WCF的“ABC”, address表明了将使用本地的名为WcfTest的专用队列。请注意, binding配置后有一个 bindingConfiguration,说明这个binding需要更高级的配置,相应的配置段在 bindings Segment中,由于示例中使用的消息队列没有使用域模式,所以securitymode设为None,该配置会将MsmqAuthenticationMode属性设置为MsmqAuthenticationMode.None。另外,配置中显示的WcfTest专用队列需要被设置为“事务性”,在创建队列的时候可以选择此属性。
配置完成后,我们可以启动该服务了:
ServiceHostserviceHost = new ServiceHost( typeof (Calculator));
serviceHost.Open();

再来看客户端,非常简单,首先在App.Config中设置“ABC”(与服务端一致):
<? xmlversion = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< system.serviceModel >
< client >
< endpointname = " CalculatorClient "
address
= " net.msmq://localhost/private/WcfTest "
binding
= " netMsmqBinding " bindingConfiguration = " msmq "
contract
= " WcfLib.ICalculate " >
</ endpoint >
</ client >

< bindings >
< netMsmqBinding >
< bindingname = " msmq " >
<securitymode="None"/>
</ binding >
</ netMsmqBinding >
</ bindings >
</ system.serviceModel >
</ configuration >

在添加了对WcfLib.dll的引用后,接下来就可以调用服务了:
ChannelFactory < WcfLib.ICalculate > channelFactory = new ChannelFactory < ICalculate > ( " CalculatorClient " );
ICalculatecalculate
= channelFactory.CreateChannel();
calculate.DealOrder( this .textBox1.Text);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值