客户端正确地关闭WCF连接

//正常调用Web服务.
StockService.StockServiceClient client = new StockService.StockServiceClient(
              "StockBasicHttpEndpoint", stockServiceUrl);
string StockId = client.GetStockIdByName("MSFT");
//服务完成后,使它关闭
try
{
   if (client.State != System.ServiceModel.CommunicationState.Faulted)
   {
      client.Close();
   }
}
catch (Exception ex)
{
   client.Abort();
}

你应该尝试在一个try-catch代码块执行Close()方法,在catch里执行Abort()方法;

不要对WCF的client类使用'using';在try-catch块里调用Close()方法关闭客户端,异常发生后调用Abort()方法。

你可能感兴趣的:(WCF)