开发者

Wrapping a .NET WCF service proxy class in a USING { } block?

开发者 https://www.devze.com 2023-01-17 12:48 出处:网络
Quick question, but is it best practice to wrap a service proxy class in a using statement? Meaning, in VS, we 开发者_Python百科generated a proxy for a WCF service. For the service class, should it be

Quick question, but is it best practice to wrap a service proxy class in a using statement? Meaning, in VS, we 开发者_Python百科generated a proxy for a WCF service. For the service class, should it be wrapped in using? I don't believe it implements IDisposable and there is no Dispose() method. Thanks.


Although proxies generated by Service Reference and svcutil.exe do implement IDisposable, in general it is not a best practice to wrap them in a using block; instead favor explicit open and closing.

Why? Well, the problem is that the Dispose method calls Close, which can throw in various circumstances, most prominently if the channel is in a Faulted state. In general this is bad, but if this happens during a finally block because you let a fault exception bubble up, it could swallow the original fault exception in favor of the Close exception, effectively losing the original exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消