WCF MaxStringContentLength error

本文关键字:error MaxStringContentLength WCF | 更新日期: 2023-09-27 18:21:44

I制作简单的WCF服务服务器端在iis上运行客户端WinForms。

当我试图将大字符串发送到服务器时,我有以下异常:

格式化程序在尝试反序列化时引发异常message:反序列化操作的请求消息正文时出错"CreateFolder"。最大字符串内容长度配额(8192)具有读取XML数据时超过。此配额可能会增加更改上的MaxStringContentLength属性创建XML读取器时使用的XmlDictionaryReaderQuotas对象。第147行,位置78。

客户端app.config:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="10000000" >
          <readerQuotas maxDepth="32" maxStringContentLength="10000000"   maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://192.168.15.72:7777/Service1.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
        name="BasicHttpBinding_IService1" />
    </client>
  </system.serviceModel>
</configuration>

服务器Web配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="10000000"
        maxBufferSize="10000000" >
          <readerQuotas maxDepth="32" maxStringContentLength="10000000"   maxArrayLength="16384"
                     maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WCFService">
        <endpoint address="" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
          name="BasicHttpBinding_IService1">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <appSettings>
    <add key="PathToSafe" value="D:'Temp"/>
  </appSettings>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

当我在localhost-is上运行服务器时,它工作得很好。有什么办法解决这个问题吗?

WCF MaxStringContentLength error

绑定适用于客户端和服务。您正确地修改了客户端,但也需要在服务器端进行修改。

客户端向服务器发送请求-服务器正在进行反序列化,并且在反序列化时发生错误。所有内容都指出,您没有更新绑定(web.config)的服务器端配置

检查您的maxReceiveMessageSize和maxBufferSize

<binding name="BasicHttpBinding_IService1"
             maxReceivedMessageSize="10000000"
             maxBufferSize="10000000">
</binding>

它们应该足够大,以允许您的较大字符串+消息中的其他内容