客户端正确地关闭WCF连接

来源:互联网 发布:anywhere虚拟定位软件 编辑:程序博客网 时间:2024/06/13 02:24
//正常调用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()方法。

原创粉丝点击