惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AWS News Blog
AWS News Blog
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
S
Secure Thoughts
L
LINUX DO - 最新话题
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Announcements
Recent Announcements
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyberwarzone
Cyberwarzone
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Vulnerabilities – Threatpost
T
Tor Project blog
GbyAI
GbyAI
B
Blog
博客园 - 司徒正美
博客园 - 叶小钗
Recorded Future
Recorded Future
F
Fortinet All Blogs
Spread Privacy
Spread Privacy
IT之家
IT之家
Forbes - Security
Forbes - Security
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
博客园 - 聂微东
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
The Blog of Author Tim Ferriss
N
News and Events Feed by Topic
O
OpenAI News
Apple Machine Learning Research
Apple Machine Learning Research
Help Net Security
Help Net Security
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 蝈蝈

LAMPR Ver 1.0 发布 - 轻松搞定LAMP环境 在Windows Server下集成Apache、Tomcat和IIS Windows Workflow 学习笔记二 序语言流行度最新排行榜,C和C++正在衰落 Windows Workflow学习笔记 EAI,ESB与SOA How to Building ASP.NET AJAX Controls 使用页面Gzip压缩提速 Microsoft AJAX Library 简介 Configuration Section Designer示例 开源的内容管理系统 Windows Live Writer截屏插件 试用Windows Live Writer写博客 wcf step by step:服务的状态 wcf step by step:如何保护你的wcf服务 wcf step by step之异常处理 wcf step by step:改进HelloWorld程序 wcf step by step:host服务与多次Endpoint wcf step by step之HelloWorld
wcf step by step:消息交换模式
蝈蝈 · 2008-03-06 · via 博客园 - 蝈蝈

wcf有三种消息交换模式,分析是oneway,request/response和duplex(双工)。其实在服务contract的定义时就已经指定了,前面没有提到是因为如果没有指定,默认是request/response的模式。这是最普通的一种模式,http就是这种消息模式。
request/response
请求-响应,如http发一个请求,iis会返回一个200表示成功。
在wcf中,客户端向服务端发一个请求,其实就是调用服务器的某一方法,服务端完成处理后,不管这个方法是否有返回值,都会给客户端发一个回应的消息,表明服务端处理完成了。request/response模式不需要特别声明,如
  [OperationContract]
  string RequestResponseMethod(string name);
 
oneway
  其实很简单,就是客户端给服务端发消息,然后服务端接到消息了自行处理,处理完不会给客户端返回任何消息。所以指定为oneway的消息模式,服务契约的接口不能包含有返回值或ref,out之类的参数。

  [OperationContract(IsOneWay=true)]
  void OnWayMethod();
注意OnWayMethod返回值必须是void,并不能带ref或out参数。其它与request/response一样。

duplex
这种模式比较特别,也非常有用。duplex会使服务端与客户端交换角色,即客户端可以向服务端主动发消息,服务端也可以与客户端主动发送消息,并且可以进行广播即给所有客户端发消息。
客户端调用服务端,服务端处理完后除被动的给客户端返回一个通知消息外,还可以主动调用客房端的方法,其实此时服务端自动变为客户端,客户端提升为服务端了,呵呵,是不是有点意思.wcf为我们做了很多,所以我们只需要简单几步就能搞定。
1.定义回调接口

[ServiceContract]
    public interface IDuplexCallback
    {
        [OperationContract]
        void HandleCallback(Employee emp);
    }

2.定义服务接口
[ServiceContract(CallbackContract = typeof(IDuplexCallback))]
    public interface IDuplexService
    {
        [OperationContract]
        void DuplexMethod();
    }

注意我们为它指定了回调的类型
3.实现服务接口
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class DuplexService : IDuplexService
    {
        #region IDuplexService Members

        public void DuplexMethod()
        {
            Console.WriteLine("DuplexMethod invoked");

            IDuplexCallback callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
            Employee emp = new Employee();
            emp.ID = 0;
            emp.Name = "0";
            callback.HandleCallback(emp);
        }

        #endregion
    }

这里需要注意ConcurrencyMode枚举值,分别是Reentrant,Multiple和Single。duplex方式一定要指定为Reentrant或Multiple方式,默认是single方式,关于它们的用法请参照其它资料。另外就是从当前上下文件获取指定类型的回调接口类型OperationContext.Current.GetCallbackChannel<T>
4.客户端
   实现回调接口
   public class DuplexCallbackServcie : IDuplexCallback
    {
        #region IDuplexCallback Members

        public void HandleCallback(Shore.cnblogs.com.WCFData.Employee emp)
        {
            Console.WriteLine("emp id ={0};name ={1}", emp.ID, emp.Name);
        }

        #endregion
    }

    客户端调用时
    DuplexCallbackServcie callback = new DuplexCallbackServcie();
                DuplexServiceProxy duplexProxy = new DuplexServiceProxy(new System.ServiceModel.InstanceContext(callback));
                duplexProxy.DuplexMethod();

    在代理对象的构造函数中指定InstanceContext对象,InstanceContext中指定回调的类型。

 
demo下载