zoukankan      html  css  js  c++  java
  • 异步调用WCF的方法需要小心的地方

    直接使用下面的代码,由于client对象占用的资源没有被释放,会导致内存泄露
    GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()
    client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
    上面的问题,可以使用下面的方法来避免

                        using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient())
                        {
                            client.computerSimA(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
                        }

    但是如果使用异步调用的方式,会导致调用不到远程服务,并且没有任何异常提示,很是让人摸不到头脑

                        using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient())
                        {
                           client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
                        }

    最后,采用了如下的代码:
                     

                        try
                        {
                            GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient();
                            client.computerSim(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
                            if (client.State != CommunicationState.Faulted) client.Close();
    
                        }
                        catch (CommunicationObjectFaultedException cofe)
                        {
                            ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + cofe.Message);
                        }
                        catch (Exception ex)
                        {
                            ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + ex.Message+f_intput.FullName);
                        }
  • 相关阅读:
    EL表达式与JSTL
    jsp
    Servlet 会话
    Servlet 常用类
    Servlet
    Java 网络编程
    CentOS系统下安装python3+Django
    转载Alpine Linux常用命令
    转载Alpine基础
    CentOS启动docker1.13失败(Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.)
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3314184.html
Copyright © 2011-2022 走看看