Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Maximum Read Depth Exception

I get the following exception when trying to pass a DTO over WCF services.

System.Xml.XmlException: The maximum read depth (32) has been exceeded because XML data being read has more levels of nesting than is allowed by the quota. This quota may be increased by changing the MaxDepth property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 5230.
   at System.Xml.XmlExceptionHelper.ThrowXmlException

The app.config binding looks like this

    <binding name="WSHttpBinding_IProjectWcfService" closeTimeout="00:10:00"
      openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="10240000" messageEncoding="Text"
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="200" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Message">

        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="">
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
        <message clientCredentialType="UserName" negotiateServiceCredential="true"
          algorithmSuite="Default" establishSecurityContext="true" />
      </security>
    </binding>

Web.config service behaviour:

    <behavior name="BehaviorConfiguration" >
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="SqlRoleProvider" />
      <serviceCredentials>
        <serviceCertificate findValue="localhost" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />

        <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="SqlMembershipProvider"/>
      </serviceCredentials>
    </behavior>

And the DTO looks like this:

[Serializable]
[DataContract(IsReference=true)]
public class MyDto
{

Any help would be appreciated as I am pulling my hair out with it.

like image 490
Burt Avatar asked Mar 25 '10 00:03

Burt


2 Answers

There's a setting called maxDepth on the <readerQuotas> which you should be able to set to a higher value than 32 (the default). You obviously have set this on the client already (to maxDepth=200), but you also need to do this on the server side - otherwise, the smaller of the two values (between client and server) will be defining the real number used.

Make sure your server side also includes these lines in its wsHttpBinding configuration:

<readerQuotas maxDepth="200" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
like image 78
marc_s Avatar answered Oct 15 '22 16:10

marc_s


You have to change the binding configuration on both the client and the server to match ...

like image 5
Joel Martinez Avatar answered Oct 15 '22 15:10

Joel Martinez