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

问题描述

限时删除!!

我们正在使用 WCF 服务

We are using WCF service

在客户端,我们计划明确关闭连接似乎有不止一种关闭方式

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

示例 1:在WCF服务消费的finally块中使用

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

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

因为如果服务处于故障状态,我们将无法调用 close()

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

示例 2:

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

在 sample2 中,我不确定如果服务处于故障状态会发生什么,关闭连接是否会引发错误?

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

推荐答案

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

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 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:27