修复了WCF 4.0 REST的XmlDictionaryReaderQuotas的最大长度配额

Tod*_*odd 8 c# rest wcf web-config

如果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>
Run Code Online (Sandbox Code Playgroud)

首先,需要增加消息大小.为此,请将maxReceivedMessageSize添加到standardEndpoint.

            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="65535" />
Run Code Online (Sandbox Code Playgroud)

要设置MaxStringContentLength,请将以下内容添加到system.serviceModel部分:

<bindings>
  <webHttpBinding>
    <binding>
      <readerQuotas maxStringContentLength="65535"/>
    </binding>
  </webHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

您需要将长度设置为适合您环境的值.