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);
                        }
  • 相关阅读:
    POI Excel表格合并,边框设置
    MYSQL中group_concat有长度限制!默认1024(转载)
    MARQUEE 字符滚动条效果
    <A>标签电子邮件链接
    <A>标签锚标记
    <hr> 水平样式分隔线
    sudo gem install cocoapods 没反应问题
    适配iPhone6和iPhone6 Plus
    同步推是如何给未越狱的IOS设备安装任意IPA的?
    据说是百度ios面试题
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3314184.html
Copyright © 2011-2022 走看看