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

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
N
Netflix TechBlog - Medium
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
S
Schneier on Security
T
Threat Research - Cisco Blogs
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
T
Tenable Blog
S
Secure Thoughts
T
Threatpost
V2EX - 技术
V2EX - 技术
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
小众软件
小众软件
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
H
Heimdal Security Blog
量子位
B
Blog

博客园 - 蝈蝈

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下载