【问题标题】:Getting 400 Bad Request when send large JSON data to WCF ReST service将大型 JSON 数据发送到 WCF ReST 服务时收到 400 Bad Request
【发布时间】:2011-09-23 16:07:29
【问题描述】:

我在服务上做了所有正确的设置,但是当我尝试将大量 JSON 数据放入我的 WCF REST 服务时,仍然得到同样愚蠢的 400 错误请求。下面是我的服务 Web.config。我已经为此苦苦挣扎了 3 周,但没有找到答案。这是响应中的异常详细信息:

“反序列化时出错 BuildStepResource 类型的对象。这 最大字符串内容长度配额 (8192) 读取时已超出 XML 数据。此配额可能会增加 通过更改 MaxStringContentLength 财产在 使用的 XmlDictionaryReaderQuotas 对象 创建 XML 阅读器时。”

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
        <bindings>
            <webHttpBinding>
                <binding name="nonSSLBinding" maxReceivedMessageSize="4194304" receiveTimeout="01:00:00" sendTimeout="01:00:00" >
                    <security mode="None">
                        <transport clientCredentialType="Ntlm"/>
                    </security>
                    <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
                </binding>
                <binding name="sslBinding" maxReceivedMessageSize="4194304" receiveTimeout="01:00:00" sendTimeout="01:00:00" >
                    <security mode="Transport">
                        <transport clientCredentialType="Basic"/>
                    </security>
                    <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
                </binding>
            </webHttpBinding>
        </bindings>
        <services>
            <service behaviorConfiguration="webBehavior" name="serviceEndpoints">
                <endpoint address="" binding="webHttpBinding" contract="ISWCRestService" bindingConfiguration="nonSSLBinding" behaviorConfiguration="customBehavior"/>
                <endpoint address="" binding="webHttpBinding" contract="ISWCRestService" bindingConfiguration="sslBinding" behaviorConfiguration="customBehavior"/>
                <endpoint address="" binding="webHttpBinding" contract="IXmlEndpoint" bindingConfiguration="nonSSLBinding" behaviorConfiguration="customBehavior"/>
                <endpoint address="" binding="webHttpBinding" contract="IXmlEndpoint" bindingConfiguration="sslBinding" behaviorConfiguration="customBehavior"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="webBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                    <dataContractSerializer maxItemsInObjectGraph="2147483646" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="customBehavior">
                    <webHttp automaticFormatSelectionEnabled="false" helpEnabled="true"/>
                    <dataContractSerializer maxItemsInObjectGraph="2147483646" />
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>

【问题讨论】:

  • 你解决过这个问题吗?我也遇到过类似的问题。

标签: wcf


【解决方案1】:

错误消息指的是在阅读时超出了配额,这让我相信您需要在 客户端 端增加 MaxStringContentLength 属性,而不是在您的网络上.config.

【讨论】:

  • 感谢您的回复。我尝试使用最大整数 maxReceivedMessageSize="2147483647",但仍然遇到相同的错误。
  • 需要在客户端进行更改。创建一个客户端应用程序,而不是只使用浏览器并尝试一下。
  • 谢谢!按照建议尝试使用客户端应用程序,但仍然遇到相同的错误。我做了一些跟踪,发现异常在服务器上,而不是客户端上。这是我的客户端配置:
  • 配置>
【解决方案2】:

我在使用 WCF 客户端时遇到了类似的问题。您必须在客户端和服务器上将 MaxItemsInObjectGraph 配置为更大。我不知道如何在 JSON 中做到这一点。

【讨论】:

  • 我在服务器上做了,但还是不行。如果我使用 REST Client for FireFox 进行测试,如何在客户端上执行此操作?或者,如果我在 jQuery 脚本中调用 PUT,我该如何设置?谢谢!
【解决方案3】:

客户端配置 -

<binding name="yourBinding"  maxReceivedMessageSize="2147483647">
          <readerQuotas
            maxDepth="2147483647"
            maxStringContentLength="2147483647"
            maxArrayLength="2147483647"
            maxBytesPerRead="2147483647"
            maxNameTableCharCount="2147483647" />
</binding>

【讨论】:

  • 配置>
  • 尝试改成WSHttpBinding
【解决方案4】:

在你的 web.config 中试试这个:

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpRuntime maxRequestLength="2147483647"/>
    </system.web>

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多