---
title: "[原创] WCF技术剖析之十：调用WCF服务的客户端应该如何进行异常处理"
author: "varsoft"
date: "2009-07-05T18:08:00Z"
tags: []
slug: "wcf-wcf"
draft: false
lastmod: "2009-07-05T18:08:00Z"
id: 871299
url: "https://varsoft.iteye.com/blog/871299"
---

在前面一片文章（[服务代理不能得到及时关闭会有什么后果?](http://www.cnblogs.com/artech/archive/2009/07/04/1516908.html)）中，我们谈到及时关闭服务代理（Service Proxy）在一个高并发环境下的重要意义，并阐明了其根本原因。但是，是否直接调用ICommunicationObject的Close方法将服务代理关闭就万事大吉了呢？事情远不会这么简单，这其中还会涉及关于异常处理的一些操作，这就是本篇文章需要讨论的话题。

**一、异常的抛出与Close的失败**

一般情况下，当服务端抛出异常，客户客户端的服务代理不能直接关闭，WCF在执行Close方法的过程中会抛出异常。我们可以通过下面的例子来证实这一点。在这个例子中，我们依然沿用计算服务的例子，下面是服务契约和服务实现的定义：

```
   1: using System.ServiceModel;
```

&lt;!--CRLF--&gt;

```
   2: namespace Artech.ExceptionHandlingDemo.Contracts
```

&lt;!--CRLF--&gt;

```
   3: {
```

&lt;!--CRLF--&gt;

```
   4:     [ServiceContract(Namespace = "urn:artech.com")]
```

&lt;!--CRLF--&gt;

```
   5:     public interface ICalculator
```

&lt;!--CRLF--&gt;

```
   6:     {
```

&lt;!--CRLF--&gt;

```
   7:         [OperationContract]
```

&lt;!--CRLF--&gt;

```
   8:         int Divide(int x, int y);
```

&lt;!--CRLF--&gt;

```
   9:     }
```

&lt;!--CRLF--&gt;

```
  10: }
```

&lt;!--CRLF--&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /\*white-space: pre;\*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } &lt;/style&gt;

```
   1: using System;
```

&lt;!--CRLF--&gt;

```
   2: using System.ServiceModel;
```

&lt;!--CRLF--&gt;

```
   3: using Artech.ExceptionHandlingDemo.Contracts;
```

&lt;!--CRLF--&gt;

```
   4: namespace Artech.ExceptionHandlingDemo.Services
```

&lt;!--CRLF--&gt;

```
   5: {
```

&lt;!--CRLF--&gt;

```
   6:     class CalcualtorService : ICalculator { public int Divide(int x, int y) { return x / y; } }
```

&lt;!--CRLF--&gt;

```
   7: }
```

&lt;!--CRLF--&gt;

为了确保服务代理的及时关闭，按照典型的编程方式，我们需要采用try/catch/finally的方式才操作服务代理对象，并把服务代理的关闭放在finally块中。WCF服务在客户端的调用程序如下所示：

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /\*white-space: pre;\*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } &lt;/style&gt;

```
   1: using System;
```

&lt;!--CRLF--&gt;

```
   2: using System.ServiceModel;
```

&lt;!--CRLF--&gt;

```
   3: using Artech.ExceptionHandlingDemo.Contracts;
```

&lt;!--CRLF--&gt;

```
   4: namespace Client
```

&lt;!--CRLF--&gt;

```
   5: {
```

&lt;!--CRLF--&gt;

```
   6:     class Program
```

&lt;!--CRLF--&gt;

```
   7:     {
```

&lt;!--CRLF--&gt;

```
   8:         static void Main(string[] args)
```

&lt;!--CRLF--&gt;

```
   9:         {
```

&lt;!--CRLF--&gt;

```
  10:             using (ChannelFactory<ICalculator> channelFatory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorservice"))
```

&lt;!--CRLF--&gt;

```
  11:             {
```

&lt;!--CRLF--&gt;

```
  12:                 ICalculator calcultor = channelFatory.CreateChannel(); try
```

&lt;!--CRLF--&gt;

```
  13:                 {
```

&lt;!--CRLF--&gt;

```
  14:                     calcultor.Divide(1, 0);
```

&lt;!--CRLF--&gt;

```
  15:                 }
```

&lt;!--CRLF--&gt;

```
  16:                 catch (Exception ex) { Console.WriteLine(ex.Message); }
```

&lt;!--CRLF--&gt;

```
  17:                 finally
```

&lt;!--CRLF--&gt;

```
  18:                 {
```

&lt;!--CRLF--&gt;

```
  19:                     (calcultor as ICommunicationObject).Close();
```

&lt;!--CRLF--&gt;

```
  20:                 }
```

&lt;!--CRLF--&gt;

```
  21:             }
```

&lt;!--CRLF--&gt;

```
  22:         }
```

&lt;!--CRLF--&gt;

```
  23:     }
```

&lt;!--CRLF--&gt;

```
  24: }
```

&lt;!--CRLF--&gt;

由于传入的参数为1和0，在服务执行除法运算的时候，会抛出DividedByZero的异常。当服务端程序执行到finally块中对服务代理进行关闭的时候，会抛出如下一个CommunicationObjectFaultedException异常，提示SerivceChannel的状态为Faulted，不能用于后续Communication。

[![image](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFWCF_F41B/image_thumb.png "image")](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFWCF_F41B/image_2.png)

**二、原理分析**

要解释具体的原因，还得从信道（Channel）的两种分类形式说起。在上面一篇文章中，我们就谈到过：WCF通过信道栈实现了消息的编码、传输及基于某些特殊功能对消息的特殊处理，而绑定对象是信道栈的缔造者，不同的绑定类型创建出来的信道栈具有不同的特性。就对会话的支持来讲，我们可以将信道分为以下两种：

- **会话信道（Sessionful Channel）：**会话信道确保客户端和服务端之间传输的消息能够相互关联，但是信道的错误（Fault）会影响后续的消息交换；
- **数据报信道（Datagram Channel）：**即使在同一个数据报信道中，每次消息的交换都是相互独立，信道的错误也不会影响后续的消息交换。

由于上面的例子中，我们采用了WsHttpBinding，所以在默认条件下创建的信道（Channel）是会话信道（Sessionful Channel）。异常抛出后，当前信道的状态将变成Faulted，表示信道出现错误。错误的信道将不能继续用于后续的通信，即使是调用Close方法试图将其关闭也不行。

也就是说异常导致信道错误（Faulted）的特性仅仅对于会话信道而言，而对于数据报信道，则没有这样的问题。对于WsHttpBinding在如下两种情况下下具有创建会话信道的能力：

- 采用任何一种非None的SecurityMode
- 采用ReliableSession

再默认的情况下，WsHttpBinding采用的SecurityMode为Message，所以其创建的信道是会话信道。如果我们将其SecurityMode设为None，则在执行Close方法的时候则不会抛出任何异常（而实际上，服务代理的关闭与否对于数据报信道来讲，没有任何意义）。具体实现如下面的代码所示：

```
   1: using System;
```

&lt;!--CRLF--&gt;

```
   2: using System.ServiceModel;
```

&lt;!--CRLF--&gt;

```
   3: using Artech.ExceptionHandlingDemo.Contracts;
```

&lt;!--CRLF--&gt;

```
   4: namespace Client
```

&lt;!--CRLF--&gt;

```
   5: {
```

&lt;!--CRLF--&gt;

```
   6:     class Program
```

&lt;!--CRLF--&gt;

```
   7:     {
```

&lt;!--CRLF--&gt;

```
   8:         static void Main(string[] args)
```

&lt;!--CRLF--&gt;

```
   9:         {
```

&lt;!--CRLF--&gt;

```
  10:             using (ChannelFactory<ICalculator> channelFatory = new ChannelFactory<ICalculator>(new WSHttpBinding(SecurityMode.None), "http://127.0.0.1:3721/calculatorservice"))
```

&lt;!--CRLF--&gt;

```
  11:             {               //......
```

&lt;!--CRLF--&gt;

```
  12:             }
```

&lt;!--CRLF--&gt;

```
  13:         }
```

&lt;!--CRLF--&gt;

```
  14:     }
```

&lt;!--CRLF--&gt;

```
  15: }
```

&lt;!--CRLF--&gt;

```
  16: 
```

&lt;!--CRLF--&gt;

**三、Close() V.S. Abort()**

在这种情况下，一般会调用另一个方法：Abort，强行中断当前信道。一般情况下，对于客户端来说，信道在下面两种情况下状态会变成Faulted：

- 调用超时，抛出TimeoutException
- 调用失败，抛出CommunicationException

所以正确的客户端进行服务调用的代码应该如下面的代码所示：通过try/catch控制服务调用，在try控制块中进行正常服务调用并正常关闭服务代理进程（调用Close方法）；在catch控制块中，捕获CommunicationException和TimeoutException这两个异常，并将服务代理对象强行关闭（调用Abort方法）。下面的代码演示了基于ChannelFactory&lt;T&gt;创建服务代理的WCF客户端编程方式，对于直接通过强类型服务代理（继承ClientBase&lt;T&gt;的服务代理类型）进行服务调用具有相同的结构。

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /\*white-space: pre;\*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } &lt;/style&gt;

```
   1: using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculateservice"))
```

&lt;!--CRLF--&gt;

```
   2: {
```

&lt;!--CRLF--&gt;

```
   3:     ICalculator calculator = channelFactory.CreateChannel();
```

&lt;!--CRLF--&gt;

```
   4:     try
```

&lt;!--CRLF--&gt;

```
   5:     {
```

&lt;!--CRLF--&gt;

```
   6:         int result = calculator.Divide(1, 0);
```

&lt;!--CRLF--&gt;

```
   7:         (calculator as ICommunicationObject).Close();
```

&lt;!--CRLF--&gt;

```
   8:     }
```

&lt;!--CRLF--&gt;

```
   9:     catch (CommunicationException ex)
```

&lt;!--CRLF--&gt;

```
  10:     {
```

&lt;!--CRLF--&gt;

```
  11:         //Exception Handling
```

&lt;!--CRLF--&gt;

```
  12:         (calculator as ICommunicationObject).Abort();
```

&lt;!--CRLF--&gt;

```
  13:     }
```

&lt;!--CRLF--&gt;

```
  14:     catch (TimeoutException ex)
```

&lt;!--CRLF--&gt;

```
  15:     {
```

&lt;!--CRLF--&gt;

```
  16:         //Exception Handling
```

&lt;!--CRLF--&gt;

```
  17:         (calculator as ICommunicationObject).Abort();
```

&lt;!--CRLF--&gt;

```
  18:     }
```

&lt;!--CRLF--&gt;

```
  19:     catch (Exception ex)
```

&lt;!--CRLF--&gt;

```
  20:     {
```

&lt;!--CRLF--&gt;

```
  21:         //Exception Handling
```

&lt;!--CRLF--&gt;

```
  22:     }
```

&lt;!--CRLF--&gt;

```
  23: }  
```

&lt;!--CRLF--&gt;

**四、通过一些编程技巧避免重复代码**

如果严格按中上面的编程方式对CommunicationException和TimeoutException进出捕获和处理，那么你的客户端代码就会到处充斥中相同的代码片断。我不知一次说过，如果你的代码中重复频率过高，或者编程人员广泛地采用Ctrl+C|Ctrl+V这样的编程方式，那么这就是你进行代码重构的信号。

为此，我们可以通过对Delegate的利用来进行代码的分离（服务调用代码和异常处理代码）。比如，我写了下面两个Invoke方法：

```
   1: using System;
```

&lt;!--CRLF--&gt;

```
   2: using System.ServiceModel;
```

&lt;!--CRLF--&gt;

```
   3: using Artech.ExceptionHandlingDemo.Contracts;
```

&lt;!--CRLF--&gt;

```
   4: namespace Client
```

&lt;!--CRLF--&gt;

```
   5: {
```

&lt;!--CRLF--&gt;

```
   6:     class Program
```

&lt;!--CRLF--&gt;

```
   7:     {
```

&lt;!--CRLF--&gt;

```
   8:         static void Invoke<TContract>(TContract proxy, Action<TContract> action)
```

&lt;!--CRLF--&gt;

```
   9:         {
```

&lt;!--CRLF--&gt;

```
  10:             try
```

&lt;!--CRLF--&gt;

```
  11:             {
```

&lt;!--CRLF--&gt;

```
  12:                 action(proxy);
```

&lt;!--CRLF--&gt;

```
  13:                 (proxy as ICommunicationObject).Close();
```

&lt;!--CRLF--&gt;

```
  14:             }
```

&lt;!--CRLF--&gt;

```
  15:             catch (CommunicationException)
```

&lt;!--CRLF--&gt;

```
  16:             {
```

&lt;!--CRLF--&gt;

```
  17:                 (proxy as ICommunicationObject).Abort();
```

&lt;!--CRLF--&gt;

```
  18:                 //Handle Exception
```

&lt;!--CRLF--&gt;

```
  19:                 throw;
```

&lt;!--CRLF--&gt;

```
  20:             }
```

&lt;!--CRL