WCF之旅 (11): 再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯

来源:百度文库 编辑:神马文学网 时间:2024/04/29 17:47:51
在一个基于面向服务的分布式环境中,借助一个标准的、平台无关的Communication Infrastructure,各个Service通过SOAP Message实现相互之间的交互。这个交互的过程实际上就是Message Exchange的过程。WCF支持不同形式的Message Exchange,我们把这称之为Message Exchange Pattern(MEP), 常见的MEP包括: Request/Reply,Request/Forget(One-way)和Duplex。通过采用Duplex MEP,我们可以实现在Service端Callback Client的操作。虽然WCF为我们实现底层的通信细节,使得我们把精力转移到业务逻辑的实现,进行Transport无关的编程,但是对底层Transport的理解有利于我们根据所处的具体环境选择一个合适的Transport。说到Transport, WCF 经常使用的是以下4个:Http,TCP,Named Pipe,MSMQ。由于不同协议自身的差异,他们对具体MEP的支持方式也会不同,我们今天就来谈谈Http和TCP对Duplex的支持。一、Sample
为了使大家对在WCF如何实现双向通信(Bidirectional Communication)有一个直观的理解,我们先来看一个简单的Sample。我们照例采用下面的4层结构和Calculator的例子:

1.Contract:Artech.DuplexWCF.Contract. ICalculator
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.DuplexWCF.Contract
{
    [ServiceContract(CallbackContract = typeof(ICallback))]
    public interface ICalculator
    {
        [OperationContract(IsOneWay = true)]
        void Add(double x, double y);
    }
}

由于模拟的是通过Callback来显示Add方法计算的结果,我把Add Operation设置成One-way。在Service Contract中设置了Callback Contract,Callback Contract定义在Interface Artech.DuplexWCF.Contract. ICallback中:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.DuplexWCF.Contract
{
    [ServiceContract]
    public interface ICallback
    {
        [OperationContract(IsOneWay = true)]
        void DisplayResult(double result);
    }
}

2.Service: Artech.DuplexWCF.Service. CalculatorService
using System;
using System.Collections.Generic;
using System.Text;
using Artech.DuplexWCF.Contract;
using System.ServiceModel;

namespace Artech.DuplexWCF.Service
{
    public class CalculatorService:ICalculator
    {
        ICalculator Members#region ICalculator Members

        public void Add(double x, double y)
        {
            double result = x + y;
            ICallback callback = OperationContext.Current.GetCallbackChannel();
            callback.DisplayResult(result);
        }

        #endregion
    }
}

在Service端,通过OperationContext.Current.GetCallbackChannel来获得Ciient指定的CallbackContext instance,进而调用Client的Operation。
3.Hosting:
Configuration:


    
        
            
                
            
        

    



我们通过netTcpBinding来模拟基于TCP的双向通信。
Program:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.DuplexWCF.Service;

namespace Artech.DuplexWCF.Hosting
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost calculatorHost = new ServiceHost(typeof(CalculatorService)))
            {
                calculatorHost.Opened += delegate
                {
                    Console.WriteLine("The calculator service has begun to listen");
                };
                calculatorHost.Open();
                Console.Read();
            }
        }
    }
}

4.Client:
Configuration:


    
        
                            bindingConfiguration="" contract="Artech.DuplexWCF.Contract.ICalculator"
                name="defaultEndpoint" />
        

    



Callback:Artech.DuplexWCF.Client. CalculatorCallback
using System;
using System.Collections.Generic;
using System.Text;
using Artech.DuplexWCF.Contract;

namespace Artech.DuplexWCF.Client
{
    public class CalculatorCallback:ICallback
    {
        ICallback Members#region ICallback Members

        public void DisplayResult(double result)
        {
            Console.WriteLine("The result is {0}", result);
        }

        #endregion
    }
}

Callback的操作-显示计算结果,实现在Artech.DuplexWCF.Client. CalculatorCallback中,他实现了在Contract中定义的Callback Contract:Artech.DuplexWCF.Contract. ICallback。
Program:
using System;
using System.Collections.Generic;
using System.Text;
using Artech.DuplexWCF.Contract;
using System.ServiceModel;

namespace Artech.DuplexWCF.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            DuplexChannelFactory channelFactory = new DuplexChannelFactory(new InstanceContext(new CalculatorCallback()),"defaultEndpoint");
            ICalculator calculator = channelFactory.CreateChannel();
            Console.WriteLine("Try to invoke the Add method");

            try
            {
                calculator.Add(1, 2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("An Exception is thrown!\n\t:Type:{0}\n\tMessage:{1}", ex.GetType(), ex.Message);
            }

            Console.Read();
        }
    }
}

在创建DuplexChannelFactory< ICalculator>中,指定了Callback Context Instance: 一个实现了Callback Contract的CalculatorCallback 对象。该对象在Service中通过OperationContext.Current.GetCallbackChannel()获得。
通过运行程序:

2. 基于Http的双向通讯V.S.基于TCP的双向通讯
由于Http和TCP在各自协议上的差异,他们实现双向通信的发式是不同的。
Http是一个应用层的协议,它的主要特征就是无连接和无状态(connectless & stateless )。它采用传统的Request/Reply的方式进行通信,Client发送Http Request请求Server的某个资源,Server端接收到该Http Request, 回发对应的Http Response。当Client端接收到对应的Response,该Connection会关闭。也就是说Client和Server的Connection仅仅维持在发送Request到接收到Response这一段时间内。同时,每次基于Http的 connection是相互独立,互不相干的,当前connection无法获得上一次connection的状态。为了保存调用的的状态信息,ASP.NET通过把状态信息保存在Server端的方式实现了对Session的支持,具体的做法是:ASP.NET为每个Session创建一个Unique ID,与之关联一个HttpSessionState对象,并把状态信息保存在内存中或者持久的存储介质(比如SQL Server)中。而WCF则采用另外的方式实现对Session的支持:每个Session关联到某个Service Instance上。
回到我们WCF双向通信的问题上,当Client调用Service之前,会有一个Endpoint在Client端被创建,用于监听Service端对它的Request。Client对Service的调用会建立一个Client到Server的Connection,当Service在执行操作过程中需要Callback对应的Client,实际上会建立另一个Service到Client的Http connection。虽然我们时候说WCF为支持双向通信提供Duplex Channel,实际上这个Duplex channel是由两个Request/Reply Channel组成的。
而对于TCP/IP簇中的传输层协议TCP,它则是一个基于Connection的协议,在正式进行数据传输的之前,必须要在Client和Server之后建立一个Connection,Connection的建立通过经典的“3次握手”来实现。TCP天生就具有Duplex的特性,也就是说当Connection被创建之后,从Client到Sever,和从Server到Client的数据传递都可以利用同一个Connection来实现。对于WCF中的双向通信,Client调用Service,Service Callback Client使用的都是同一个Connection、同一个Channel。所以基于TCP的Duplex Channel才是真正意义上的Duplex Channel。