-->

“The maximum string content length quota (8192) ha

2019-02-11 18:03发布

问题:

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.

回答1:

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.



回答2:

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"



回答3:

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>


回答4:

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



回答5:

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/



回答6:

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.