当前位置:编程学习 > 网站相关 >>

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

在一个基于面向服务的分布式环境中,借助一个标准的、平台无关的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<ICallback>();
            callback.DisplayResult(result);
        }

        #endregion
    }
}

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

3.Hosting:

Configuration:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>       
        <services>
            <service name="Artech.DuplexWCF.Service.CalculatorService">
                <endpoint address="net.tcp://localhost:9999/calculator"  binding="netTcpBinding" contract="Artech.DuplexWCF.Contract.ICalculator" />               
            </service>
        </services>
    </system.serviceModel>
</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:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="net.tcp://localhost:9999/calculator" binding="netTcpBinding"
                bindingConfiguration="" contract="Artech.DuplexWCF.Contract.ICalculator"
                name="defaultEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

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 Sys

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,