您的位置:首页 > 其它

WCF的消息交换模式

2009-06-20 20:25 375 查看
本文的出发点

通过阅读本文,您能理解以下知识:

WCF定义了哪几种消息交换模式?
One-Way Calls
Request/Reply
Duplex
用示例来解析WCF的消息交换模式

本文适合的读者

本文涉及到了SOA中的消息交换的基础概念,需要一些初级的Xml Web Service和分布式系统开发的经验,最好理解WCF架构

WCF定义了哪几种消息交换模式?

WCF定义了三种消息交换方式 ,分别为:

One-Way Calls
Request/Reply
Duplex

One-Way Calls

在几种消息交换模式中,one-way calls是最没良心的,对于客户端,one-way calls就如肉包子打狗,有去无回。下面的图示给出这种交换模型的特征:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;

namespace Jillzhang.Messaging.Service

Request/reply的Contract接口定义如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;

namespace Jillzhang.Messaging.Service

Duplex的交换模式需要现定义Callback的Contract接口,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;
using System.ServiceModel;

namespace Jillzhang.Messaging.Service

下面,我们来看一下,如何创建承载服务的应用程序,首先在app.config做如下配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:6987/Service/duplex" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/oneway" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.IOneWayJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:6987/Service/normal" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" contract="Jillzhang.Messaging.Contract.INormalJob"
name="NetTcpBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

需要注意的是:在设定Duplex模式时,如果服务端采用的是WsDualHttpBinding,而不是本文中的NetTcpBinding,最好指定以下clientBaseAddress,默认情况下,clientBaseAddress会尝试用80端口,可通常情况80端口都是被占用,你需要设置一个其他端口。

因为回调的Contract实现是在客户端的,所以需要在客户端实现1个ICallback实现,代码如下:

下面是客户端调用的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jillzhang.Messaging.Contract;

namespace Jillzhang.Messaging.Client

首先运行服务承载程序Jillzhang.Messaging.Host,然后运行客户端

会产生如下的结果:

服务端运行解图



客户端运行解图:



本文参考资料 

http://msdn.microsoft.com/msdnmag/issues/06/10/wcfessentials/default.aspx
http://www.rainsts.net/article.asp?id=428

本文相关示例文件

示例项目:/Files/jillzhang/Jillzhang.Messaging.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: