Maximum String Content Length and readerQuotas Element

While working on a WCF service, I received this error message when trying to return long string data from a method:

An exception of type 'System.ServiceModel.Dispatcher.NetDispatcherFaultException' occurred in mscorlib.dll but was not handled in user code

Additional information: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:ClientPrintResult. 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 2, position 40523.'.  Please see InnerException for more details.

Being somewhat new to WCF, I assumed there was some configuration I could set somewhere to address this issue.  And after a bit of searching, I found the right configuration element: readerQuotas.  Here's a segment of a client side configuration file with this element in context:

   1: <system.serviceModel>
   2:    <bindings>
   3:       <wsHttpBinding>
   4:          <binding name="MyService" maxReceivedMessageSize="2147483647">
   5:             <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
   6:                           maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
   7:                           maxNameTableCharCount="2147483647" />
   8:             <security mode="TransportWithMessageCredential">
   9:                <transport clientCredentialType="None" />
  10:                <message clientCredentialType="UserName" />
  11:             </security>
  12:          </binding>
  13:       </wsHttpBinding>
  14:    </bindings>

 


In this configuration sample,  all of the readerQuotas attributes are set to ridiculously large values, which may not be the best choice from a security perspective.  The documentation for this element says that these constraints exist to help prevent denial of service attacks.   One interesting thing to note is that this element is required only on client side configuration, not on the server.


Another related attribute is the maxReceivedMessageSize attribute on the binding element, which sets the maximum message size that can be processed.  Like the readerQuotas element, it is only necessary to set this in client configuration.

11 comments: (+add yours?)

Anonymous said...

Hi, I am using Microsoft Live Translation Service and facing same issue.

I went through you article and done configuration accrodingly.

still i am getting same error.

please let me know you take on this.
thanks,
Sanket

Arnold Zokas said...

Hi Joe,

Thanks for posting this. I was having the same problem.

Regards,
Arnold

Unknown said...

Thanks a lot!

Additional keywords for search engines: NotFound

Anonymous said...

Thank you very much!

Raj said...

This worked like champ.

Thanks alot mate.

-Raj

Anonymous said...

this configuration needs to be on server side as well..

B

Naveen M said...

Hello, I am getting same error while using Microsoft translator in windows application.

Please post complete step by step fix for this.
I have configured above said configuration but I could not succeeded.
Thanks In Advance
Naveen M

Anonymous said...

" it is only necessary to set this in client configuration" - WRONG. Spent hours trying to "fix" clinet side. Server side binding was the problem.

Kaustubh said...

Thanks Joe, This blog helped us lot.
Keep sharing knowledge. :)
Thanks again.

Anonymous said...

Cheers :)

Anonymous said...

What happens if the client is a Java application trying to invoke a .NET WCF web service? What settings should be changed?