Category nameļ¼šMessages

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();

 

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta