关闭WCF连接 [英] Closing WCF connection
问题描述
我们在客户端使用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屋!