现在的位置: 首页 > 综合 > 正文

[转载]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication)

2012年08月01日 ⁄ 综合 ⁄ 共 8865字 ⁄ 字号 评论关闭
http://www.cnblogs.com/artech/archive/2007/03/02/661969.html

昨天写了一篇Remoting中如何实现双向通信的文章《[原创].NET Remoting: 如何通过Remoting实现双向通信(Bidirectional Communication) 》,作为对比,今天我们来讨论一下WCF的双向通信。

为了使我们能够更好地对比双向通信在Remoting中和WCF中的实现,我们的Sample采用一样的业务逻辑——调用一个数学计算的远程调用,除了传递相应的操作数之外,我们还传递一个对象,这个对象可以在Server端中回调 (Callback) 把运算结果在Client端显示出来。
可以通过下面的URL下载源代码:
http://www.cnblogs.com/files/artech/Artech.WCFService.2007.03.02.zip

Step1:构建整个Solution的整体构架。


整个Solution的架构在我的之前的Blog有了详细的介绍([原创]我的WCF之旅(1):创建一个简单的WCF程序),这里只作一个简单的介绍。

  • Artech.WCFService.Contract: Class Library Project,用来保存Contract(Service Contact、Message Contract、Data Contract), 之所以把Contract独立出来的原因是考虑到他同时被Server端——Service本身和Service Hosting和Client端使用
  •  Artech.WCFService.Service:Class Library Project,Service的业务逻辑, 这个Project引用Artech.WCFService.Contract Project和System.ServiceModel DLL。
  •  Artech.WCFService.Hosting:Console Application, 用于以Self-Hosting的方式Host Service。这个Project引用Artech.WCFService.Contract和Artech. Project WCFService.Service。Project和System.ServiceModel DLL。
  •  Artech.WCFService.Client:Console Application, 用以模拟现实中的调用Service的Clinet。这个Project引用Artech.WCFService.Contract Project 和System.ServiceModel DLL。
  •  http://localhost/WCFService: Web Site Project, 用于模拟如何把Service Host到IIS中。这个Project引用Artech.WCFService.Contract、Artech.WCFService.Service和System.ServiceModel DLL。

Step 2 在Artech.WCFService.Contract定义Calculator Service 和Callback的Contract

1.IDuplexCalculator.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.WCFService.Contract
{
     [ServiceContract(CallbackContract 
= typeof(ICalculatorCallback))]
    
public interface IDuplexCalculator
    
{
         [OperationContract]
        
void Add(double x, double y);
    }

}

2.ICalculatorCallback.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.WCFService.Contract
{
    [ServiceContract]
    
public interface ICalculatorCallback
    
{
        [OperationContract]
        
void ShowResult(double x, double y, double result);
    }

}

这里有以下几点需要注意的:

 

1.在一个分布式的环境中,Client能够调用Service,它必须知道Service的Contract, Contract定义了Service暴露给外界的所有可用的Operation,以及这些Operation的签名(Signature).至于Service中定义的Opertion采用怎样的实现,Client不需要了解。这也是在WCF中把Service Contract与具体的Service Implementation相互分离的一个重要原因——我们把Contract单独提取出来,把他暴露给Client,从而可以避免把过多的暴露业务逻辑的实现。

2.在一个分布式的环境中,Serer端和Client并不是一成不变的,他们是可以相互转化的。提供服务的就是Server,消费Service的就是Client。在这个例子中,当Artech.WCFService.Client调用Host在Artech.WCFService.Hosting中的DuplexCalculatorService(定义在 Artech.WCFService.Service中),Artech.WCFService.Client是Client,而Server端的执行环境是Artech.WCFService.Hosting。而当Calculator Service回调(Callback)Client的逻辑把运算结果显示出来时候,因为Callback的逻辑是在Artech.WCFService.Client中执行的,所以Artech.WCFService.Client成了Server,而CalculatorCallbackHandler(定义在 Artech.WCFService.Client中)成了真正的Service。

3.我们已经说过Client能够调用Service,它必须知道Service的Contract。所以DuplexCalculatorService能过Callback Artech.WCFService.Client,它也必须知道回调操作的Contract。WCF通过在ServiceContractAttribute中的CallbackContrac参数在制定。

 [ServiceContract(CallbackContract = typeof(ICalculatorCallback))]
    
public interface IDuplexCalculator
    
{
         [OperationContract]
        
void Add(double x, double y);
    }

Step 3 在Artech.WCFService.Service定义Duplex Calculator Service

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    
public class DuplexCalculatorService:IDuplexCalculator
    
{
        
IDuplexCalculator Members
    }

这里有以下几点需要注意的:

1. 必须把并发模式ConcurrencyMode设置为ConcurencyMode. Reentrant 或者ConcurencyMode.Multiple。要弄清种种的原因,我们先来看看在本例中的具体的消息交互的情况(假设我们的调用Duplex Calculator Service 和回调都采用Request/Response的Message Excahnge Pattern,时间上一般这种情况我们应该采用One-way的ME):


首先Client调用Duplex Calculator Service, Service Request从Client到Service,Service开始执行运算,运算完成后Callback Client将运算结构在Client端显示出来,这个过程中Service向Client发送一个Callback Message,等Client完成Callback操作后,会向Service端发送一个Callback Response(实际上是一个空的Message——以为Callback操作没有返回值),Service收到Callback Response之后,会执行后续的操作,等所有的操作执行完毕,会发送Service Response(这里也是一个空的Message)到Client。

现在我们 来看看为什么在建立Duplex Service的时候要把并发模式设为ConcurencyMode. Reentrant 或者ConcurencyMode.Multiple。在默认的并发模式下(ConcurencyMode.Single),WCF为了保证其线程安全性(Thread Safety),在整个调用Service的过程中,InstanceContext会被WCF锁住(Lock)。一本Sample为例,从Client向Service发送Service Request 到手的Server发回的Service Resqonse,整个InstanceContext会在Server端被锁住, 由于在Client执行的Callback操作使用的是同一个InstanceContext, 这样形成了一个死锁(DeadLock)——Calculator Service必须等Callback操作完成之后才能执行后续的操作,而Callback操作必须等待InstanceContext被解锁(Unlock)才能执行,然而InstanceContext却被Calculator Service锁住。

当ConcurencyMode为ConcurencyMode. Reentrant 或者ConcurencyMode.Multiple的时候。当Serivice向外调用某个操作(outgoing call)的时候,或者说在向外发送一个Outgoing Message的时候,WCF会解锁(Unlock)InstanceContext。以本例为例,Service 回调Client的时候,原来被锁住的InstanceContext会被解锁。这样Callback操作就可以利用InstanceContext来执行。

2. Service可以通过OperationContext.Current.GetCallbackChannel<T>() 来或者Client在调用Calculator Service时指定的Callback Object。其中T一般被指定为Callback Contract对应的Type。 

ICalculatorCallback callback = OperationContext.Current.GetCallbackChannel<ICalculatorCallback>();

Step 4 在Artech.WCFService.Hosting中Host Duplex  Calculator  Service

<?xml version="1.0" encoding="utf-8" ?>
<configuration>  
    
<system.serviceModel>
        
<behaviors>
            
<serviceBehaviors>
                
<behavior name="calculatorServieBehavior">
                    
<serviceMetadata httpGetEnabled="true" />
                
</behavior>
            
</serviceBehaviors>
        
</behaviors>      
        
<services>            
            
<service behaviorConfiguration="calculatorServieBehavior" name="Artech.WCFService.Service.DuplexCalculatorService">
                
<endpoint binding="wsDualHttpBinding" contract="Artech.WCFService.Contract.IDuplexCalculator">                  
                
</endpoint>
                
<host>
                    
<baseAddresses>
                        
<add baseAddress="http://localhost:7777/DuplexCalculator" />
                    
</baseAddresses>
                
</host>
            
</service>            
        
</services>
    
</system.serviceModel>
</configuration>

Program.cs 

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.WCFService.Contract;
using Artech.WCFService.Service;
using System.ServiceModel.Description;

namespace Artech.WCFService.Hosting
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            HostDuplexCalculator();
        }


       
static void HostDuplexCalculator()
        
{
            
using (ServiceHost calculatorSerivceHost = new ServiceHost(typeof(DuplexCalculatorService)))
            
{
                calculatorSerivceHost.Opened 
+= delegate
                
{
                    Console.WriteLine(
"Duplex calculator Service has begun to listen  ");
                }
;

                calculatorSerivceHost.Open();

                Console.Read();
            }

        }
        
    }

}

这里需要注意的时候,在Host Duplex  Calculator Service 的时候,我们要为它添加相应的Endpoint。对于支持双向通信的Service,它对Endpoint有一定的要求——我们必须为它指定一个支持Duplex MEP(Message Exchange Pattern)的Binding——比如wsDualHttpBinding,netDualTcpBinding。这里我们使用的时wsDualHttpBinding。

Step 5 在Artech.WCFService.Client定义Callback对象和调用Duplex  Calculator  Service 

DuplexCalculatorClient.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

using Artech.WCFService.Contract;

namespace Artech.WCFService.Client
{
    
class DuplexCalculatorClient:ClientBase<IDuplexCalculator>,IDuplexCalculator
    
{

        
public DuplexCalculatorClient(InstanceContext callbackInstance)
            : 
base(callbackInstance)
        
{ }

        
IDuplexCalculator Members
    }

}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<bindings>
      
<wsDualHttpBinding>
          
<binding name="wsDualBinding_IDuplexCalculator" clientBaseAddress="http://localhost:6666/myClient/" />
      
</wsDualHttpBinding>
    
</bindings>
    
<client>      
      
<endpoint address="http://localhost:7777/DuplexCalculator" binding="wsDualHttpBinding"
        bindingConfiguration
="wsDualBinding_IDuplexCalculator" contract="Artech.WCFService.Contract.IDuplexCalculator"
        name
="duplexCalculatorEndpoint" />
      
<endpoint address="http://localhost/WCFService/SessionfulCalculatorService.svc"
        binding
="wsHttpBinding" bindingConfiguration="" contract="Artech.WCFService.Contract.ISessionfulCalculator" />
    
</client>
  
</system.serviceModel>
 
</configuration>

CalculatorCallbackHandler.cs

using System;
using System.Collections.Generic;
using System.Text;
using Artech.WCFService.Contract;

namespace Artech.WCFService.Client
{
    
class CalculatorCallbackHandler:ICalculatorCallback

抱歉!评论已关闭.