如果POST主体的长度大于8192个字符,则WCF 4.0 REST项目将返回400 Bad Request错误。这是XmlDictionaryReaderQuotas.MaxStringContentLength属性的默认值。 XmlDictionaryReader类用于反序列化过程,即使对于JSON消息也是如此。
我见过很多关于如何使用自定义绑定和端点为WCF解决此问题的示例,但没有使用简化配置的WCF 4.0 REST项目的解决方案。
普通的web.config文件包含一个如下所示的部分:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
首先,需要增加邮件大小。为此,请将maxReceivedMessageSize添加到standardEndpoint。
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="65535" />
要设置MaxStringContentLength,请将以下内容添加到system.serviceModel部分:
<bindings>
<webHttpBinding>
<binding>
<readerQuotas maxStringContentLength="65535"/>
</binding>
</webHttpBinding>
</bindings>
您需要将长度设置为适合您环境的值。