Wcf Client 异常和关闭的通用处理方法
2013-07-09 12:13
162 查看
在项目中采用wcf通讯,客户端很多地方调用服务,需要统一的处理超时和通讯异常以及关闭连接。
1.调用尝试和异常捕获
首先,项目中添加一个通用类ServiceDelegate.cs
View Code
参考链接:
http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue
http://stackoverflow.com/questions/6130331/how-to-handle-wcf-exceptions-consolidated-list-with-code
1.调用尝试和异常捕获
首先,项目中添加一个通用类ServiceDelegate.cs
/// <summary> /// 调用SysSvc服务,统一处理TimeoutException和CommunicationException异常 /// 用法: /// List<ModuleEO/> list = SvcClient.Instance.SysClientCall(q => q.GetAllModules()); /// </summary> /// <typeparam name="TResult">返回值</typeparam> /// <param name="func">调用的方法</param> /// <returns>泛型返回值</returns> public TResult SysClientCall<TResult>(Func<SysClient, TResult> func) { var client = this.GetSysClient(); try { TResult rec = func.Invoke(client); client.Close(); return rec; } catch (TimeoutException ex) { //服务器超时错误,提示用户即可。 client.Abort(); MessageBox.Show("服务器通讯超时,请重新尝试。"); } catch (CommunicationException ex) { //服务器连接通讯异常,提示用户即可。 client.Abort(); MessageBox.Show("服务器通讯错误,请重新尝试。"); } catch (Exception ex) { //未处理异常,重新抛出 client.Abort(); throw; } return default(TResult); }
View Code
参考链接:
http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue
http://stackoverflow.com/questions/6130331/how-to-handle-wcf-exceptions-consolidated-list-with-code
相关文章推荐
- Tomcat启动错误(8080端口被占用)处理方法(关闭windows端口通用方法)
- 8080端口被占用 Tomcat启动错误 处理方法(关闭windows端口通用方法)
- js弹出层包含flash 不能关闭隐藏的2种处理方法
- java对世界各个时区(TimeZone)的通用转换处理方法
- [置顶] hadoop无法正常关闭尝试处理方法
- 基于Java子线程中的异常处理方法(通用)
- win7系统下 vs2010 调式就关闭要重启处理方法
- 操作系统处理内存超载的通用方法
- AOI中如何设计通用的图像处理方法
- “Microsoft Office Word 遇到问题需要关闭。我们对此引起的不便表示抱歉”处理方法之一
- 使用Watin关闭浏览器时遇到弹出确认框的处理方法
- 处理编码和日期的通用字符串转换方法
- asp关闭错误处理方法
- linux下open too many files错误Socket未正确关闭的处理方法
- lamada形式处理递归的通用方法
- 通用产品设计的巨大障碍--软件配置数据--的通用化处理方法
- python处理excel进程无法关闭的方法---这个方法可行
- 在Django的通用视图中处理Context的方法
- C# UDP Socket 远程主机强行关闭了一个连接的处理方法
- 通用的XML处理方法