Jim*_*mmy 5 .net wcf idisposable
假设我从同步版本开始:
using(var svc = new ServiceObject()) {
var result = svc.DoSomething();
// do stuff with result
}
Run Code Online (Sandbox Code Playgroud)
我结束了
var svc = new ServiceObject();
svc.BeginDoSomething(async => {
var result = svc.EndDoSomething(async);
svc.Dispose();
// do stuff with result
},null);
Run Code Online (Sandbox Code Playgroud)
1)这是调用Dispose()的正确位置吗?
2)有没有办法使用using()?
来自Rotem Bloom的博客:http: //caught-in-a-web.blogspot.com/2008/05/best-practices-how-to-dispose-wcf.html
最佳实践:如何处置WCF客户端
对于Dispose WCF客户端,不建议使用using语句(在Visual Basic中使用).这是因为using语句的结束可能会导致异常,这些异常可能会掩盖您可能需要了解的其他异常.
using (CalculatorClient client = new CalculatorClient())
{
...
} // this line might throw
Console.WriteLine("Hope this code wasn't important, because it might not happen.");
The correct way to do it is:
try
{
client.Close();
}
catch (CommunicationException)
{
client.Abort();
}
catch (TimeoutException)
{
client.Abort();
}
catch
{
client.Abort();
throw;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |