C# – “The maximum string content length quota (8192) has been exceeded while reading XML data” error while sending XML string to WCF

cnetquotawcfweb.config

I am working with a .NET, C# application which intends to send a long XML string to a WCF Service method for further operation. When my application tries to send the XML string to WCF Service in runtime, I am getting a error message :

"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:strProdUserDataXML. The InnerException message was 'There was an error deserializing the object of type System.String. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 131, position 57.'. Please see InnerException for more details."

My application side web.config I have written the "binding" & "endpoint" as:

<binding name="EndPointHTTPGenericPortal" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    <security mode="None">
    <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    </binding>

    <endpoint address="http://192.168.140.40/WcfGenericPortal_Service/Service1.svc" binding="basicHttpBinding" bindingConfiguration="EndPointHTTPGenericPortal" contract="IService1" name="EndPointHTTPGenericPortal" behaviorConfiguration="Graph" />

If any body can help me on how to solve this error I will be very much obliged.
Thanks to all in advance.

Best Answer

Here is an article on MSDN about Reader Quotas.

It appears that one of the reader quotas on your server side is being exceeded.

Specifically, maxStringContentLength is being exceeded. The default value is 8192 characters for maxStringContentLength which as described by the error message is being exceeded.

But it may not be the best approach to just bump up all the values to the maximum 2147483647 as some others have suggested.

As written in the MSDN documentation that I linked:

The complexity constraints provide protection from denial of service (DOS) attacks that attempt to use message complexity to tie up endpoint processing resources. Other complexity constraints include items such as a maximum element depth and a maximum length for string content within the message.

Coupled with the fact that you currently have Security Mode set to None - you may be setting yourself up for some problems.