21

I am working with a .NET, C# application which intends to send a long XML string to a WCF Service method for further operation. When my application tries to send the XML string to WCF Service in runtime, I am getting a error message :

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

My application side web.config I have written the "binding" & "endpoint" as:

<binding name="EndPointHTTPGenericPortal" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    <security mode="None">
    <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    </binding>

    <endpoint address="http://192.168.140.40/WcfGenericPortal_Service/Service1.svc" binding="basicHttpBinding" bindingConfiguration="EndPointHTTPGenericPortal" contract="IService1" name="EndPointHTTPGenericPortal" behaviorConfiguration="Graph" />

If any body can help me on how to solve this error I will be very much obliged. Thanks to all in advance.

Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
Pinaki Karuri
  • 221
  • 1
  • 2
  • 5
  • And what is your server-side web config? – Alex Mar 27 '13 at 08:59
  • 2
    What is the value for maximum string content length quota on the server side? – Jocke Mar 27 '13 at 09:01
  • 1
    Here is a related Q&A http://stackoverflow.com/questions/6917061/the-maximum-string-content-length-quota-8192 – Jocke Mar 27 '13 at 09:04
  • Hi Voo & Jocke, thanks for your reply. server-side config is: ' ' – Pinaki Karuri Mar 27 '13 at 09:19
  • where is your 'readerQuotas' node? – Piotr Justyna Mar 27 '13 at 09:22
  • Many here are just going with the brute force solution which is to set all of the config values to the maximum of 2 GB (2147483647 bytes), but beware that this may make you vulnerable to DoS attacks, etc. It will work, but there is a reason why the defaults are set so low in comparison. – Derek W Aug 17 '13 at 14:46
  • In regards to my previous comment - sorry, some are character, array length values, and node depth value - not just byte values in the reader quota config settings. – Derek W Aug 17 '13 at 15:03
  • I have same issue. except that in my case this happen only on 1 of 4 dev computer which are Dell manufacturer computer which are same exact models. we all use same visual studio and only 1 guys it crashes when we all run the same solution i made. it is on SVN so same exact code. all running ANY CPU. – Franck Jan 02 '14 at 19:14
  • possible duplicate of [The maximum string content length quota (8192) has been exceeded while reading XML data](http://stackoverflow.com/questions/6600057/the-maximum-string-content-length-quota-8192-has-been-exceeded-while-reading-x) – Ryan Gates Jan 20 '15 at 14:45

7 Answers7

11

Here is an article on MSDN about Reader Quotas.

It appears that one of the reader quotas on your server side is being exceeded.

Specifically, maxStringContentLength is being exceeded. The default value is 8192 characters for maxStringContentLength which as described by the error message is being exceeded.

But it may not be the best approach to just bump up all the values to the maximum 2147483647 as some others have suggested.

As written in the MSDN documentation that I linked:

The complexity constraints provide protection from denial of service (DOS) attacks that attempt to use message complexity to tie up endpoint processing resources. Other complexity constraints include items such as a maximum element depth and a maximum length for string content within the message.

Coupled with the fact that you currently have Security Mode set to None - you may be setting yourself up for some problems.

Derek W
  • 9,708
  • 5
  • 58
  • 67
8

I got this error and solve by adding this - MaxItemsInObjectGraph property for the service in both the client and the server configuration .

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

server side

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="Service.Service1Behavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
</system.serviceModel>

Client side

<behaviors >
  <endpointBehaviors>
    <behavior name="endpointbehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

and don't forget to apply this behavior to EndPoint behaviorConfiguration="endpointbehaviour"

Lingaraj
  • 452
  • 3
  • 14
7

Client Side Binding

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService11" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
messageEncoding="Text">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647"
maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>

<behaviors>
<endpointBehaviors>
<behavior name="KAMServiceDistributor">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:1234/xxxx/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService11"
contract="yourservice namespae" name="AnyName" />
</client>
</system.serviceModel>

Service Config File:

<system.serviceModel>
<behaviors>


<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer ignoreExtensionDataObject="false" maxItemsInObjectGraph="2147483646" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding <b>maxReceivedMessageSize="2147483647"</b>>
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="Service">
<endpoint binding="basicHttpBinding" contract="IService" />
</service>
</services>
</system.serviceModel>
6

Try to set following things in bindings.

<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
    maxArrayLength="2147483647" maxBytesPerRead="2147483647"
    maxNameTableCharCount="2147483647" />

It has solved my problem. For more reference find following link http://blogfornet.com/2013/08/the-maximum-string-content-length-quota-8192-has-been-exceeded-while-reading-xml-data/

laalto
  • 150,114
  • 66
  • 286
  • 303
5

Pinaki Karuri,

quotas lengths depend not only on the client's configuration - they also depend on the server's one. Please post your WCF server's web.config so we can shed some light on the problem. There is a probability you already have the quota set there for 8192, so the quickest way for you would be to find and increase its value.

Update

As far as I can see, you are missing 'readerQuotas' node from your server's web.config, so MaxStringContentLength has its value set to default (8192). Please refer to this link for more information: http://msdn.microsoft.com/en-us/library/system.xml.xmldictionaryreaderquotas.maxstringcontentlength.aspx

Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
1

Server side

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding maxBufferPoolSize="2147483647"
                     maxReceivedMessageSize="2147483647"
                     maxBufferSize="2147483647">
                <readerQuotas maxDepth="200"
                              maxStringContentLength="2147483647"
                              maxArrayLength="16384"
                              maxBytesPerRead="2147483647"
                              maxNameTableCharCount="16384" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

Client side

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IRequestService" allowCookies="true"
        maxReceivedMessageSize="2147483647"
        maxBufferSize="2147483647"
        maxBufferPoolSize="2147483647">
                <readerQuotas maxDepth="32"
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"/>
            </binding>
            <binding name="BasicHttpBinding_IAttachmentService" allowCookies="true"
        maxReceivedMessageSize="2147483647"
        maxBufferSize="2147483647"
        maxBufferPoolSize="2147483647">
                <readerQuotas maxDepth="32"
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"/>
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>
Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98
0

Check that your Target Framework for the client is the same as that of the service. I had this issue and tried all of the above fixes but that did not work. Checked properties and checked the Target Framework and changed it.

Luigi
  • 1