WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]

简介: 原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]在进行基于会话信道的WCF服务调用中,由于受到并发信道数量的限制,我们需要及时的关闭信道;当遇到某些异常,我们需要强行中止(Abort)信道,相关的原理,可以参考我的文章《服务代理不能得到及时关闭会有什么后果?》。
原文: WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]

在进行基于会话信道的WCF服务调用中,由于受到并发信道数量的限制,我们需要及时的关闭信道;当遇到某些异常,我们需要强行中止(Abort)信道,相关的原理,可以参考我的文章《服务代理不能得到及时关闭会有什么后果?》。在真正的企业级开发中,正如我们一般不会让开发人员手工控制数据库连接的开启和关闭一样,我们一般也不会让开发人员手工去创建、开启、中止和关闭信道,这些工作是框架应该完成的操作。这篇文章,我们就来介绍如果通过一些编程技巧,让开发者能够无视“信道”的存在,像调用一个普通对象一样进行服务调用。

一、正常的服务调用方式

如果通过ChannelFactory<TChannel>创建用于服务调用的代理,下面的代码片段描述了客户端典型的服务调用形式:将服务调用在基于代理对象的using块中,并通过try/catch进一步对服务调用操作进行异常处理。当TimeoutException或者CommunicationException被捕获后,调用Abort方法将信道中止。当程序执行到using的末尾,Dispose方法会进一步调用Close方法对信道进行关闭。

class Program
{
    static void Main(string[] args)
    {
        using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
        {
            ICalculator calculator = channelFactory.CreateChannel();
            using (calculator as IDisposable)
            {
                try
                {
                    Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.Add(1, 2));
                }
                catch (TimeoutException)
                {
                    (calculator as ICommunicationObject).Abort();
                    throw;
                }
                catch (CommunicationException)
                {
                    (calculator as ICommunicationObject).Abort();
                    throw;
                }
            }
        }
 
        Console.Read();
    }
}

二、借助通过Delegate实现异常处理和服务代理的关闭

虽然上面的编程方式是正确的服务调用方式,但是在真正的应用中,如果在每处进行服务调用的地方都采用上面的方式,在我看来是不能容忍的。这不但会让你的程序显得臃肿不堪,而且带来非常多重复的代码,此外频繁创建ChannelFactory<TChannel>对性能也会有影响。我们可以通过一些公共个方法实现对重复代码(ChannelFactory<TChannel>的创建,服务调用的创建、中止和关闭,以及异常处理)。为此我创建了如下一个ServiceInvoker类型,通过两个重载的Invoke方法实现对目标服务的调用。

   1: using System;
   2: using System.Collections.Generic;
   3: using System.ServiceModel;
   4: namespace Artech.Lib
   5: {
   6:     public class ServiceInvoker
   7:     {
   8:         private static Dictionary<string, ChannelFactory> channelFactories = new Dictionary<string, ChannelFactory>();
   9:         private static object syncHelper = new object();
  10:  
  11:         private static ChannelFactory<TChannel> GetChannelFactory<TChannel>(string endpointConfigurationName)
  12:         {
  13:             ChannelFactory<TChannel> channelFactory = null;
  14:             if (channelFactories.ContainsKey(endpointConfigurationName))
  15:             {
  16:                 channelFactory = channelFactories[endpointConfigurationName] as ChannelFactory<TChannel>;
  17:             }
  18:  
  19:             if (null == channelFactory)
  20:             {
  21:                 channelFactory = new ChannelFactory<TChannel>(endpointConfigurationName);
  22:                 lock (syncHelper)
  23:                 {
  24:                     channelFactories[endpointConfigurationName] = channelFactory;
  25:                 }
  26:             }
  27:             return channelFactory;
  28:         }
  29:  
  30:         public static void Invoke<TChannel>(Action<TChannel> action, TChannel proxy)
  31:         {
  32:             ICommunicationObject channel = proxy as ICommunicationObject;
  33:             if (null == channel)
  34:             {
  35:                 throw new ArgumentException("The proxy is not a valid channel implementing the ICommunicationObject interface", "proxy");
  36:             }
  37:             try
  38:             {
  39:                 action(proxy);
  40:             }
  41:             catch (TimeoutException)
  42:             {
  43:                 channel.Abort();
  44:                 throw;
  45:             }
  46:             catch (CommunicationException)
  47:             {
  48:                 channel.Abort();
  49:                 throw;
  50:             }
  51:             finally
  52:             {
  53:                 channel.Close();
  54:             }
  55:         }
  56:  
  57:         public static TResult Invoke<TChannel, TResult>(Func<TChannel, TResult> function, TChannel proxy)
  58:         {
  59:             ICommunicationObject channel = proxy as ICommunicationObject;
  60:             if (null == channel)
  61:             {
  62:                 throw new ArgumentException("The proxy is not a valid channel implementing the ICommunicationObject interface", "proxy");
  63:             }
  64:             try
  65:             {
  66:               return  function(proxy);
  67:             }
  68:             catch (TimeoutException)
  69:             {
  70:                 channel.Abort();
  71:                 throw;
  72:             }
  73:             catch (CommunicationException)
  74:             {
  75:                 channel.Abort();
  76:                 throw;
  77:             }
  78:             finally
  79:             {
  80:                 channel.Close();
  81:             }
  82:         }
  83:  
  84:         public static void Invoke<TChannel>(Action<TChannel> action, string endpointConfigurationName)
  85:         {
  86:             Guard.ArgumentNotNullOrEmpty(endpointConfigurationName, "endpointConfigurationName");
  87:             Invoke<TChannel>(action, GetChannelFactory<TChannel>(endpointConfigurationName).CreateChannel());
  88:         }
  89:  
  90:         public static TResult Invoke<TChannel, TResult>(Func<TChannel, TResult> function, string endpointConfigurationName)
  91:         {
  92:             Guard.ArgumentNotNullOrEmpty(endpointConfigurationName, "endpointConfigurationName");           
  93:             return Invoke<TChannel, TResult>(function, GetChannelFactory<TChannel>(endpointConfigurationName).CreateChannel());
  94:         }        
  95:     }
  96: }

 

处于对性能的考虑,避免对ChannelFactory<TChannel>的频繁创建,通过一个字典对象将创建出来的ChannelFactory<TChannel>缓存起来;两个Invoke方法中,服务的调用通过两个Delegate对象(Action<TChannel>和Func<TChannel, TResult>)表示,另一个参数表示终结点的配置名称。那么这时的服务调用就会变得相当简单:

   1: using System;
   2: using Artech.Lib;
   3: using Artech.WcfServices.Contracts;
   4: namespace Artech.WcfServices.Clients
   5: {
   6:     class Program
   7:     {
   8:         static void Main(string[] args)
   9:         {
  10:             int result = ServiceInvoker.Invoke<ICalculator, int>(calculator => calculator.Add(1, 2), "calculatorservice");
  11:             Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, result);
  12:             Console.Read();
  13:         }
  14:     }
  15: }

三、对ServiceInvoker的改进

实际上,为了对服务调用实现细节进行进一步的封装,一般地我们可以将其定义在一个独立的层中,比如服务代理层(这里的层不一定像数据访问层、业务逻辑层一样需要一个明显的界限,这里可能就是一个单独的类型而已)。在这种情况下,我们可以上面的ServiceInvoker方法进行一定的改造,使之更加符合这种分层的场景。上面我们调用静态方法的形式进行服务的调用,现在我们需要的是:实例化服务代理对象,并调用相应的方法。为此,我创建了一个泛型的ServiceInvoker<TChannel>类型,该类型继承自上述的ServiceInvoker,泛型类型表示服务契约类型。ServiceInvoker<TChannel>定义如下:

   1: using System;
   2: namespace Artech.Lib
   3: {
   4:     public class ServiceInvoker<TChannel>:ServiceInvoker
   5:     {
   6:         public string EndpointConfigurationName
   7:         {get; private set;}
   8:  
   9:         public ServiceInvoker(string endpointConfigurationName)
  10:         {
  11:             Guard.ArgumentNotNullOrEmpty(endpointConfigurationName, "endpointConfigurationName");
  12:             this.EndpointConfigurationName = endpointConfigurationName;
  13:         }
  14:  
  15:         public void Invoke(Action<TChannel> action)
  16:         {
  17:             Invoke<TChannel>(action, this.EndpointConfigurationName);
  18:         }
  19:  
  20:         public TResult Invoke<TResult>(Func<TChannel, TResult> function)
  21:         {
  22:             return Invoke<TChannel, TResult>(function, this.EndpointConfigurationName);
  23:         }
  24:     }
  25: }

通过传入终结点配置名称创建ServiceInvoker<TChannel>对象,直接通过调用基类的静态方法实现了两个Invoke方法。

在分层设计中,为每一个层定义的组件创建基类是一个很常见的设计方式。在这里,假设所有的服务代理类型均继承自基类:ServiceProxyBase<TChannel>,泛型类型为服务契约类型。同样通过传入终结点配置名称创建服务代理,并借助于通过Invoker属性表示的ServiceInvoker<TChannel>对象进行服务的调用。ServiceProxyBase<TChannel>定义如下:

   1: namespace Artech.Lib
   2: {
   3:     public class ServiceProxyBase<TChannel>
   4:     {
   5:         public virtual ServiceInvoker<TChannel> Invoker
   6:         { get; private set; }
   7:  
   8:         public ServiceProxyBase(string endpointConfigurationName)
   9:         {
  10:             Guard.ArgumentNotNullOrEmpty(endpointConfigurationName, "endpointConfigurationName");
  11:             this.Invoker = new ServiceInvoker<TChannel>(endpointConfigurationName);
  12:         }
  13:     }
  14: }

那么,具体的服务代理类型就可以通过如下的方式定义了:

   1: using Artech.Lib;
   2: using Artech.WcfServices.Contracts;
   3: namespace Artech.WcfServices.Clients
   4: {
   5:     public class CalculatorProxy : ServiceProxyBase<ICalculator>, ICalculator
   6:     {
   7:         public CalculatorProxy():base(Constants.EndpointConfigurationNames.CalculatorService)
   8:         { }
   9:  
  10:         public int Add(int x, int y)
  11:         {
  12:             return this.Invoker.Invoke<int>(calculator => calculator.Add(x, y));
  13:         }
  14:     }
  15:  
  16:     public class Constants
  17:     {
  18:         public class EndpointConfigurationNames
  19:         {
  20:             public const string CalculatorService = "calculatorservice";
  21:         }
  22:     }
  23: }

那么现在服务代理的消费者(一般是Presenter层对象),就可以直接实例化服务代理对象,并调用相应的方法(这里的方法与服务契约方法一致)即可,所有关于服务调用的细节均被封装在服务代理中。

   1: using System;
   2: using Artech.Lib;
   3: using Artech.WcfServices.Contracts;
   4: namespace Artech.WcfServices.Clients
   5: {
   6:     class Program
   7:     {
   8:         static void Main(string[] args)
   9:         {
  10:             CalculatorProxy calculatorProxy = new CalculatorProxy();
  11:             int result = calculatorProxy.Add(1, 2);
  12:             Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, result);
  13:             Console.Read();
  14:         }
  15:     }
  16: }

四、局限

这个解决方案有一个很大的局限:服务方式不能包含ref和out参数,因为这两种类型的参数不能作为匿名方法的参数。

作者: Artech
出处: http://artech.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
目录
相关文章
一起谈.NET技术,基于CallContextInitializer的WCF扩展导致的严重问题
  WCF是一个具有极高扩展度的分布式通信框架,无论是在信道层(Channel Layer)还是服务模型层(Service Model),我们都可以自定义相关组件通过相应的扩展注入到WCF运行环境中。在WCF众多可扩展点中,ICallContextInitializer可以帮助我们在服务操作执行前后完成一些额外的功能,这实际上就是一种AOP的实现方式。
852 0
|
索引
一起谈.NET技术,Silverlight+WCF 新手实例 象棋 主界面-棋谱-回放-结局(四十)
查看本系列其他相关文章请点击:Silverlight+WCF 新手实例象棋专题索引 在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 在Silverlight+WCF 新手实例 象棋 主界面-棋谱-回放(三十九)中,我们实现了用户的棋谱回放,在文章的下面,我们曾留下了两...
920 0
|
网络协议
一起谈.NET技术,使命必达:深入剖析WCF的可靠会话
  作为一个通信基础平台,WCF必须保证通信的可靠性。由于消息交换是WCF采用的通信手段,通信可靠性的保障体现在确保消息的可靠传输。WCF本质上是一个消息处理框架,作为整个消息交换系统的两个终端,即发送端和接收端。
960 0
一起谈.NET技术,Siverlight与WCF通信之双工netTcp实现视频对话
  效果   先看看效果再说,基本逻辑是两个人通过Silverlight端,借助TCP协议分别向服务器不断传输视频,服务器接收到视频后,会检测这些视频是发给谁的,然后回调某个客户端来接收并显示这些视频。
1058 0
|
Windows
一起谈.NET技术,WCF使用NetTcp传输文件
  服务实现   服务中有一个上传二进制流的方法UpLoad: [ServiceContract]public interface IAddService{  [OperationContract]  void UpLoad(byte[] file);}   (为了减少时间,采用了一点硬编...
917 0
|
消息中间件
一起谈.NET技术,NET下RabbitMQ实践 [WCF发布篇]
  在之前的两篇文章中,主要介绍了RabbitMQ环境配置,简单示例的编写。今天将会介绍如何使用WCF将RabbitMQ列队以服务的方式进行发布。  注:因为RabbitMQ的官方.net客户端中包括了WCF的SAMPLE代码演示,很适合初学者,所以我就偷了个懒,直接对照它的SAMPLE来说明了,算是借花献佛吧,呵呵。
1159 0
|
前端开发 .NET C#
一起谈.NET技术,WCF+WF双剑合璧构建微软的SOA系列(一):从一个简单的Demo开始
  本系列文章将从实例出发,以实例结尾。由浅入深讲解在我们项目中如何使用WCF和WF。我们会发现使用WCF+WF将造就出其他技术无法达到的高度。最后我会将程序架到云端。   微软.net的3W(WPF、WCF、WF)战略如下图。
1220 0
|
XML 网络协议 数据格式
一起谈.NET技术,当Silverlight同时遇上TCP和HTTP的WCF服务
  如果只是单一的TCP通信   如果你的silverlight应用因为一些特殊原因跟WCF通信时使用的不是Http协议,而是TCP协议,并且是Host到控制台上的。那么假设是下面这个简单的服务:   1,WCF中的主机服务代码如下: class Program {static voi...
809 0
一起谈.NET技术,使用WCF实现SOA面向服务编程——简单的WCF开发实例
  前面为大家介绍过WCF的特点,现在再讲解一下WCF基础概念。   在WCF里,各个Application之间的通信是由EndPoint来实现的,EndPoint是WCF实现通信的核心要素。一个WCF Service可由多个EndPoint集合组成,每个EndPoint只能有一种绑定,就是说EndPoint就是通信的入口,客户端和服务端通过 EndPoint交换信息。
938 0
|
JSON 前端开发 数据格式
一起谈.NET技术,使用WCF实现SOA面向服务编程——使用AJAX+WCF服务页面开发
  在上一篇讲到,如果将BLL层的每一个类都转化为*.svc,这是一个不实在的想法。它会使服务变化复杂,难于管理。   这时候,我们第一时间想到的是23个简单开发模式中的Factory,在这里,Factory正好派上用场。
890 0