【问题标题】:WCF MaxStringContentLength errorWCF MaxStringContentLength 错误
【发布时间】:2013-07-08 10:07:48
【问题描述】:

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

当我尝试向服务器发送大字符串时,出现以下异常:

格式化程序在尝试反序列化 消息:反序列化操作请求消息正文时出错 '创建文件夹'。最大字符串内容长度配额 (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>

服务器网络配置:

<?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 iis 上运行服务器时,它工作得很好。 任何想法如何解决这个问题?

【问题讨论】:

  • 重复可能在这里看到这个链接,stackoverflow.com/questions/6600057/…
  • 请注意,如果您不将该绑定定义分配给端点(通过endpoint 元素上的bindingConfiguration 属性),更改将不会生效。将使用端点指定绑定的默认值。

标签: c# wcf


【解决方案1】:

检查您的 maxReceiveMessageSize 和 maxBufferSize

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 你能告诉我这是怎么配置的吗?我不知道该怎么做
    • 找到 web.config 文件。找到“bindings\basicHttpBinding”部分,然后将客户端的“binding”节点复制粘贴到服务中找到的匹配节点上。如果您仍然不确定 - 复制/粘贴 web.config 并更新您的帖子,我会发布一个答案应该是什么样子...
    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多