zoukankan      html  css  js  c++  java
  • 调用WCF出现的异常

    使用如下代码调用调用远程服务时,
                       try
                        {
                            using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient())
                            {
                                client.computerSim(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
                            }
                        }
                        catch (CommunicationObjectFaultedException cofe)
                        {
                            ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + cofe.Message);
                        }
                        catch (Exception ex)
                        {
                            ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + ex.Message+f_intput.FullName);
                        }

    出现如下异常提示:
    通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态
    并被CommunicationObjectFaultedException捕获到。在MSDN查找,由于关闭client时出现异常会导致CommunicationObjectFaultedException抛出。

    这是由于使用了using 关键字,在离开using的作用域时,会调用dispose方法来释放资源,在强制关闭cilent时,抛出了CommunicationObjectFaultedException异常。这是由于使用了using关键字来清理资源,在清理资源时导致异常,把using关键字移除,更能直接找到导致异常的原因。在移除using之后,系统抛出了如下异常:
    The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:fileStrings. The InnerException message was 'There was an error deserializing the object of type System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'.  Please see InnerException for more details。
    到这里异常已经很明显了,传输的数据超出了WCF最大允许的长度。这个错误本来应该很容易被修正,但是因为使用Using,导致不能找到出错的正确位置,而花费了相当多的时间。的如下配置(红色字体),异常就没有发生了:
                     <bindings>
       <netTcpBinding>
        <binding name="GetSimService" closeTimeout="00:10:00" openTimeout="00:10:00"
                        receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false"
                        transferMode="Buffered" transactionProtocol="OleTransactions"
                        hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                        maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100"
                        maxReceivedMessageSize="2147483647">
         <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
         <reliableSession ordered="true" inactivityTimeout="00:10:00"
                            enabled="false" />
         <security mode="Transport">
          <transport clientCredentialType="Windows" protectionLevel="EncryptA

    ndSign" />
          <message clientCredentialType="Windows" />
         </security>
        </binding>
       </netTcpBinding>
      </bindings>

  • 相关阅读:
    计算机程序的构造和解释
    StackOverflow之旅<1>------{去掉烦人的"!=null"判断}
    tomcat 启动显示指定的服务未安装
    Sql Server 表的复制
    Sql Server配置管理器与 Sql Server Management Studio
    “因为数据库正在使用,所以无法获得对数据库的独占访问权。”处理
    Win10磁贴 横向排列 增加多行 多列 磁贴横向展示
    解决使用Hyper-v 设置虚拟机网卡出现-从网络文件夹中隐藏
    Hyper-V网络设置(物理机+虚拟机)
    易语言等软件自动获取管理员权限,在64位Windows7系统非管理员帐户中执行
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3314290.html
Copyright © 2011-2022 走看看