Loading ...

WCF: There was an error deserializing the object of type xxxx. The maximum string content length quota (8192) has been exceeded while reading XML data | CodeAsp.Net

WCF: There was an error deserializing the object of type xxxx. The maximum string content length quota (8192) has been exceeded while reading XML data

 /5
0 (0votes)

While working with one POST method of my WCF library I was getting the following error:
There was an error deserializing the object of type xxxx. 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. 

Fix:

Make a custom WebHttpBinding class:

    /// <summary>
    /// A binding used to configure endpoints for Web services that are exposed through HTTP requests instead of SOAP messages.
    /// </summary>
    public class MyWebHttpBinding : WebHttpBinding
    {
        public MyWebHttpBinding()
        {
            this.MaxReceivedMessageSize = int.MaxValue;
            this.ReaderQuotas.MaxArrayLength = int.MaxValue;
            this.ReaderQuotas.MaxStringContentLength = int.MaxValue;
            this.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
        }

    }


Your hostfactory should look like this:

    public class MyHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new MyHost(serviceType, baseAddresses);
        }
    }


Here comes the main thing to notice, your Host:

public class MyHost : ServiceHost
    {
        readonly Type contractType;
        public MyHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            this.contractType = serviceType;
        }

        protected override void OnOpening()
        {
            ServiceEndpoint endpoint = this.AddServiceEndpoint(this.contractType, 
                new MyWebHttpBinding(), "");
        }
    }



After adding the custom WebHttpBinding instance to our endpoint our issue will be fixed.

Comments (no comments yet)

Top Posts