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);
                        }
  • 相关阅读:
    SAP资产变动明细
    SAP资产明细报表
    SAP连接HANA数据库
    工程变更记录报表
    SELECTION-SCREEN 文本丢失
    FG函数模块
    DOI EXCEL显示报表
    OLE填充EXCEL
    下载模板,上传EXCEL
    SELECTION-SCREEN 加按钮
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3314184.html
Copyright © 2011-2022 走看看