WCF and large messages

Published 08-20-2008 1:46 PM | Ramon Smits

Today I got this nice InvalidOperationException:

System.InvalidOperationException: There is an error in XML document (11, 11657). ---> System.Xml.XmlException: 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.


The reason was that the message contained an element that had more then 8k of textual data as seen in the exception message. Problem was that I could not adjust this as I am using WCF and it is using an XML reader internally. Luckily MSDN comes to the rescue:

I now set the values in code:

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = 256*1024;
binding.ReaderQuotas.MaxStringContentLength = 64 * 1024;

The only thing is that you need to reference System.Runtime.Serialization to set it from code.

Filed under: , , ,

Comments

# Patrick Wellink said on August 21, 2008 8:24 AM:

Hey Ramon,

Have a look at this post : bloggingabout.net/.../beware-of-default-settings-in-wcf.aspx

I believe tip 4 : davidpallmann.spaces.live.com/.../cns!E95EF9DC3FDB978E!260.entry

covers this blog entry. Have a look cause there are several other things you can run into......

# Robbert Hock said on August 21, 2008 12:40 PM:

Hi Ramon,

I advise you to set the quota by adjusting the binding settings in the configuration file instead of doing it in code.

Like this:

<configuration>

 <system.serviceModel>

  <bindings>

     <basicHttpBinding>

       <binding name="Binding1">

         <readerQuotas maxBytesPerRead ="1000"

                       maxDepth="100"/>

         <security mode="None" />

       </binding>

     </basicHttpBinding>

   </bindings>

 </system.serviceModel>

</configuration>

Last year I also had this problem, I than adjusted it in the configuration file which was the only thing that I had to deploy, instead of completely doing a rebuild and deploy of my code.

gr,

Robbert