http://www.cnblogs.com/artech/archive/2009/07/05/1517257.html
在(服务代理不能得到及时关闭会有什么后果?) 中,我们谈到及时关闭服务代理(Service Proxy)在一个高并发环境下的重要意义,并阐明了其根本原因。但是,是否直接调用ICommunicationObject的Close方法将服务代 理关闭就万事大吉了呢?事情远不会这么简单,这其中还会涉及关于异常处理的一些操作,这就是本篇文章需要讨论的话题。
一、异常的抛出与Close的失败
一般情况下,当服务端抛出异常,客户客户端的服务代理不能直接关闭,WCF在执行Close方法的过程中会抛出异常。我们可以通过下面的例子来证实这一点。在这个例子中,我们依然沿用计算服务的例子,下面是服务契约和服务实现的定义:
using System.ServiceModel;
namespace Artech.ExceptionHandlingDemo.Contracts
3: {
)]
interface ICalculator
6: {
7: [OperationContract]
int y);
9: }
10: }
using System;
using System.ServiceModel;
using Artech.ExceptionHandlingDemo.Contracts;
namespace Artech.ExceptionHandlingDemo.Services
5: {
return x / y; } }
7: }