WCF从理论到实践(7):消息交换模式 (转)
2008-06-05 13:56
381 查看
WCF从理论到实践(7):消息交换模式
本文的出发点
通过阅读本文,您能理解以下知识:
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
本文的出发点
通过阅读本文,您能理解以下知识:
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
相关文章推荐
- WCF从理论到实践(7):消息交换模式
- WCF从理论到实践(7):消息交换模式
- 艾伟:WCF从理论到实践(7):消息交换模式
- WCF从理论到实践(7):消息交换模式
- WCF面向服务应用程序系列之十六:消息交换模式(MEP)-请求/应答操作
- WCF分布式安全开发实践(8):消息安全模式之用户名身份验证:Message_UserName_WSHttpBinding
- WCF从理论到实践(9):实例模式和对象生命周期 (转)
- WCF消息交换模式之双工通讯(Duplex)
- WCF从理论到实践(9):实例模式和对象生命周期
- WCF消息交换模式:数据报模式
- WCF初探-5:WCF消息交换模式之双工通讯(Duplex)
- WCF系列教程之WCF消息交换模式之单项模式
- WCF从理论到实践(9):实例模式和对象生命周期
- WCF分布式安全开发实践(10):消息安全模式之自定义用户名密码:Message_UserNamePassword_WSHttpBinding
- WCF学习总结——WCF消息交换模式
- WCF面向服务应用程序系列之十八:消息交换模式(MEP)-回调操作
- WCF分布式安全开发实践(12):消息安全模式之自定义X509证书验证:Message_CustomX509Certificate_WSHttpBinding
- 艾伟:WCF从理论到实践(9):实例模式和对象生命周期
- WCF消息交换模式之Duplex
- WCF分布式安全开发实践(9):消息安全模式之Windows身份验证:Message_Windows_NetTcpBinding