c# - 使用 Delphi 使用 WCF - 最大字符串内容长度配额 (8192) 错误

标签 c# wcf delphi soap web-config

我有一个由 Delphi 应用程序使用的 WCF。有时在向服务器发送大请求时会出现此错误:

---------------------------
Debugger Exception Notification
---------------------------
Project Project43.exe raised exception class ERemotableException with message 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Log'. 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. Line 58, position 140.'.
---------------------------
Break   Continue   Help   
---------------------------

我在服务器端配置了 web.config 如下:


Web.config

<?xml version="1.0" ?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service name="NewServiceType">
        <clear />
        <endpoint address="http://localhost" binding="basicHttpBinding" bindingConfiguration="" contract="IRoboConsultaLog" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

但我一直收到这个错误。网上有人建议客户端的app.config也要修改,但是我用的是Delphi,不知道怎么修改。

我还注意到我不知道如何正确配置 <endpoint>标签,也许这就是我所有麻烦的原因。下面是我的 web 服务的接口(interface)和类(简化为更清晰):


接口(interface):

namespace RoboConsultaLogServer
{
    [ServiceContract]
    public interface IRoboConsultaLog
    {
        [OperationContract]
        void Log(string key, string numeroSerial, string nomeTarefa, int qtdProcessos, float uptime, float duracaoTarefa,
                 int qtdSucesso, int qtdInsucesso, int qtdCancelado, bool servico, bool notificarResponsaveis, string logProcessos);
    }
}

public class RoboConsultaLog : IRoboConsultaLog 
{
    ...
}

有人知道如何解决这个问题吗?

最佳答案

如您所见,这是因为端点配置:

<endpoint address="http://localhost" binding="basicHttpBinding" bindingConfiguration="" contract="IRoboConsultaLog" />

基本上没有使用您的读者配额配置。

绑定(bind)参数仅定义绑定(bind)类型,如果您想传递绑定(bind)配置,则必须填写 BindingConfiguration 参数。绑定(bind)类型与您的案例中的配置名称相同只是巧合(“basicHttpBinding”)。所以试试这个(为清楚起见,我更改了名称):

    <bindings>
      <basicHttpBinding>
        <binding name="myBindingConfiguration">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <endpoint address="http://localhost" binding="basicHttpBinding" bindingConfiguration="myBindingConfiguration" contract="IRoboConsultaLog" />

编辑:此外,如果您发送如此多的数据,也许最好将其作为文件发送,将其保存在 session 中,然后在 WCF 方法中使用它。数据将在没有 WCF 开销的情况下发送,并且 WCF 调用会快得多,从而使应用程序响应更快。此外,它也不太容易出现 WCF 超时。

关于c# - 使用 Delphi 使用 WCF - 最大字符串内容长度配额 (8192) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851575/

相关文章:

c# - C# 中是否有任何函数返回与卡方拟合优度检验相关的 p 值?

c# - WCF:什么是 ServiceHost?

c# - 将启用 Ajax 的 WCF 服务公开给 C# 代码

delphi - 字符串和整数访问和保存

c# - ModelState 错误 - Newtonsoft.Json.JsonSerializationException : Cannot populate list type System.Net.TrackingStringDictionary

c# - 锁定是否确保从缓存中清除读取和写入?如果是这样,如何?

c# - 在 C# 中,哪个包含装箱和拆箱策略,数组还是数组列表?

c# - 从 WCF 中的抽象类继承而不公开该类

delphi - 使用递归函数进行数制转换 - 返回值错误

Delphi Indy TCP 连接到 RECON