关闭WCF连接 [英] Closing WCF connection

查看:120
本文介绍了关闭WCF连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在客户端使用WCF服务



我们计划明确关闭连接
似乎有多种方式关闭



Sample1:
在WCF服务消耗的finally块中使用

  if(client.State == CommunicationState.Faulted)
{
client.Abort();
}
client.Close();

由于如果服务处于故障状态,我们将无法调用close >

Sample2:

 使用(ClientProxy proxy = new ClientProxy $ b {
//调用您的服务方法
}

解决方案

第二个示例(第二个示例)如果服务处于故障状态,使用使用块不正确。使用块确保在代理对象上调用Dispose方法。 Dispose方法又调用Close方法,该方法将(尝试)连接到在通信状态故障时抛出异常的服务。所以你的感觉/预感是绝对正确的。如果代理Dispose方法使用你的第一个示例中的代码,但它不是所以不使用使用块)将是很好的)。


We are using WCF service

on the client side we are planning to explicitly close the connection It seems there are more then one way of closing

Sample1: In the finally block of the WCF service consumption use

if (client.State == CommunicationState.Faulted)
{
  client.Abort();
}
client.Close();

Since if the service is in fault state we will not be able to call close()

Sample2:

using(ClientProxy proxy = new ClientProxy())
{   
  //call your service methods
}

in sample2 i am not sure what will happen if the service is in fault state, will it throw error closing the connection?

解决方案

The 2nd sample using the "using" block is incorrect. The using block ensures that the Dispose method is called on the proxy object. The Dispose method in turn calls the Close method which will (try to) connect to the service which will throw an exception when the communication state is faulted. So your feelings/hunch are absolutely right. It would be nice if the proxy Dispose method used the code from your first sample but it doesn't so don't use the using block :)

这篇关于关闭WCF连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆