Monday, July 28, 2014

WCF – The maximum string content length quota (8192) has been exceeded while reading XML data

The DataContractSerializer in WCF serializes objects to XML Streams. There is a size (length) limitation on these streams. They are restricted by default to 8k (8192 bytes).

While this helps in avoiding Denial of Service (Dos) attacks, it is an inconvenience for several service operations that DO return LARGER than 8k size objects. The result set from a single query returned by a WCF service could constitute one such large object. When this happens, the WCF run-time throws an exception:

The maximum string content length quota (8192) has been exceeded while reading XML data.

To work around this limitation, certain web.config values need to be tweaked – both, on the client web.config (Web.Config file of application which consume WCF Service/Web Service) as well as the server(Web.Config file of Web Service/WCF Service). Just having a client SEND a large message – without the server web.config configured to ACCEPT the message, will just throw a different exception. One needs to tweak both in conjunction as described below in Server Web.Config file:

Snapshot of Server Web.Config file:

<bindings>
<basicHttpBinding>
<binding name="basicHttpConfiguration" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>

Server (Web Service/WCF) Web.Config file summary

  • maxStringContentLength
  • maxBufferSize
  • maxReceivedMessageSize

Client Web.Config file:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="RequestProcessorBinding" receiveTimeout="00:30:00"
sendTimeout="00:01:00" maxBufferSize="2147483647"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.mydomain.com/wcfservice/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="MyWcfServiceReference.IMyService" name="BasicHttpBinding_IMyService" />
</client>
</system.serviceModel>

Client Side Web.Config Summary

  • maxStringContentLength
  • maxBufferSize
  • maxBufferPoolSize
  • maxReceivedMessageSize
  • maxStringContentLength