WCF中的Dispose(转)
毫无疑问,在.NET Framework中,一个资源(尤其是非托管资源)通常都需要实现IDisposable接口。一旦实现了该接口,我们就可以使用using语句来管理资源,这是最便捷的方式。但是,一旦在using语句中抛出了异常,就可能不会正确完成资源的回收,尤其是连接,很可能会一直打开,既占用了通道和端口,还可能出现资源的浪费,从而影响系统的性能和稳定性。
微软推荐的最佳实践是抛弃using语句,转而利用 try/catch(/finally)语句。它要求在try语句中调用Close()方法,而在catch中调用Abort()方法。在新闻中已经说明了Close()与Abort()方法的区别,即后者可以强制地关闭客户端,包括关闭客户端连接,释放资源。由于Close()方法可能会抛出 CommunicationException和TimeoutException异常,通常的客户端代码应该是这样:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
var myClient = new MyClient();try{ //其他代码 myClient.Close();}catch (CommunicationException){ myClient.Abort();}catch (TimeoutException){ myClient.Abort();}catch (Exception){ myClient.Abort(); throw;} |
在最后增加对Exception异常的捕获很有必要,因为我们不知道Close()方法会否抛出某些不可预知的异常,例如 OutOfMemoryException等。新闻中提到Steve Smith的方法其实就是对这段冗长代码的封装,封装方式是采用扩展方法,扩展的类型为ICommunicationObject。这是因为所有的客户端对象都实现了ICommunicationObject接口。以下是Steve Smith的扩展方法代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
public static class Extensions{ public static void CloseConnection(this ICommunicationObject myServiceClient) { if (myServiceClient.State != CommunicationState.Opened) { return; } try { myServiceClient.Close(); } catch (CommunicationException ex) { Debug.Print(ex.ToString()); myServiceClient.Abort(); } catch (TimeoutException ex) { Debug.Print(ex.ToString()); myServiceClient.Abort(); } catch (Exception ex) { Debug.Print(ex.ToString()); myServiceClient.Abort(); throw; } }} |
利用该扩展方法,在本应调用Close()方法的地方,代替为CloseConnection()方法,就可以避免写冗长的catch代码。而使用 Lambda表达式的方式可以说是独辟蹊径,使用起来与using语法大致接近。实现方法是定义一个静态方法,并接受一个 ICommunicationObject对象与Action委托:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class Util{ public static void Using(T client, Action action) where T : ICommunicationObject { try { action(client); client.Close(); } catch (CommunicationException) { client.Abort(); } catch (TimeoutException) { client.Abort(); } catch (Exception) { client.Abort(); throw; } }} |
使用时,可以将原本的客户端代码作为Action委托的Lambda表达式传递给Using方法中:
|
1
2
3
4
5
6
7
8
9
|
Util.Using(new MyClient(), client => { client.SomeWCFOperation(); //其他代码; }); |
http://tech.ddvip.com/2009-03/1236930451111278.html
posted on 2014-04-16 14:20 ExplorerMan 阅读(240) 评论(0) 收藏 举报
浙公网安备 33010602011771号