Ramon Smits

Tell me your secrets and i'll tell you mine

Recent Posts

Tags

Community

Email Notifications

Patterns & Practices / Guidelines

EntLib

Nant

Blogs that I monitor

Archives

WCF and large messages

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.

Example client code:

 

        var binding = new BasicHttpBinding
        {
            MaxReceivedMessageSize = 256 * 1024,
            ReaderQuotas = new XmlDictionaryReaderQuotas
            {
                MaxStringContentLength = 64 * 1024
            }
        };

        var channelFactory = new ChannelFactory(
                binding,
                new EndpointAddress(new Uri(_address), new SpnEndpointIdentity(string.Empty))
                );

        var channel = channelFactory.CreateChannel();

Example server code:

 

        var binding = new BasicHttpBinding
        {
            MaxReceivedMessageSize = 256 * 1024,
            ReaderQuotas = new XmlDictionaryReaderQuotas
            {
                MaxStringContentLength = 64 * 1024
            }
        };

        var baseAddress = new Uri("http://localhost:666");
        var serviceHost = new ServiceHost(new MyInterfaceImplementation());
        serviceHost.AddServiceEndpoint(typeof(IMyServiceInterface), binding, baseAddress);
        serviceHost.Open();

 

Comments

Patrick Wellink said:

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......

# August 21, 2008 8:24 AM

Robbert Hock said:

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

# August 21, 2008 12:40 PM

dalvir said:

hi.

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

binding.MaxReceivedMessageSize = 256*1024;

binding.ReaderQuotas.MaxStringContentLength = 64 * 1024;

After using this code .. Error remains same.

# May 7, 2010 2:39 PM

Ramon Smits said:

@dalvir: What error? Can you specify the exception details?

# May 7, 2010 3:27 PM

manjunath said:

hey roman,

Where To write below code?

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

binding.MaxReceivedMessageSize = 256*1024;

binding.ReaderQuotas.MaxStringContentLength = 64 * 1024;

can you give example?

# June 1, 2010 2:09 PM

Ramon Smits said:

Added example code to the article

# June 1, 2010 2:26 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 7 and 4 and type the answer here: