WCF 消息交换模式 转
2012-04-18 09:29
134 查看
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实现,代码如下:
下面是客户端调用的代码:
客户端代码
首先运行服务承载程序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
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实现,代码如下:
下面是客户端调用的代码:
客户端代码
首先运行服务承载程序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
相关文章推荐
- WCF学习笔记之消息交换模式
- WCF面向服务应用程序系列之十六:消息交换模式(MEP)-请求/应答操作
- WCF 消息交换模式
- WCF初探-3:WCF消息交换模式之单向模式
- WCF从理论到实践(7):消息交换模式
- WCF 之 消息交换模式
- WCF消息交换模式之Duplex
- WCF初探-3:WCF消息交换模式之单向模式
- WCF初探-4:WCF消息交换模式之请求与答复模式
- WCF初探-5:WCF消息交换模式之双工通讯(Duplex)
- WCF从理论到实践(7):消息交换模式
- 【菜鸟学WCF】Wcf消息交换模式
- WCF面向服务应用程序系列之十七:消息交换模式(MEP)-单向操作
- WCF入门(三)——消息交换模式
- WCF学习总结——WCF消息交换模式
- WCF面向服务应用程序系列之十八:消息交换模式(MEP)-回调操作
- wcf 基础知识 之 消息交换模式 response/reply oneway deplex
- WCF消息交换模式之双工通讯(Duplex)
- WCF从理论到实践(7):消息交换模式 (转)
- WCF初探-4:WCF消息交换模式之请求与答复模式