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);
                        }
  • 相关阅读:
    Solr简介
    儿童节快乐
    添加新的内容分类
    weka
    Junit测试样例
    Linux MySQL单实例源码编译安装5.5.32
    perconatoolkit 工具集安装
    Linux MySQL单实例源码编译安装5.6
    MySQL 开机自启动
    mysql5.6之前需要账号的安全加固
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3314184.html
Copyright © 2011-2022 走看看