Recently, while working with the WCF services I faced a problem which seemed to be very common but the solution to it is not clearly available. Let me explain what the problem is all about, when you try to consume your WCF service in another project like Silverlight, WPF, ASP.Net, etc; sometimes when you request the service for some data you may see the following error message:

“Error in deserializing body of reply message for operation ‘Your Method Name’. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.”

This happens because of the size of response message i.e. when your service prepares the result into a byte[] as response to your request, it finds out that the “Maximum Array Length” is set to 16384 and since your response byte[] would be greater than this limit it throws an exception. In order to cure this problem, open the config file of the project where this WCF service is consumed – ServiceReferences.ClientConfig (Silverlight), app.config (WPF) or web.config(ASP.Net). Here you’ll see the binding settings of the service (please note: you may see multiple bindings, one for each service you have consumed. In this case, identify the binding of the service which is giving this exception). In the setting you’ll see something similar to below:


<binding name="CustomBinding_Testervice">
	<binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" maxSessionSize="2048">
		<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
			maxBytesPerRead="4096" maxNameTableCharCount="16384" />
	</binaryMessageEncoding>
	<httpTransport manualAddressing="false" maxBufferPoolSize="524288" 
		maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" 
		bypassProxyOnLocal="false" decompressionEnabled="true" 
		hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536"
		proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered"
		unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" />
</binding>

Over here you’ll see that the value of “maxArrayLength” is set to “16384”. Just increase this length (you can change other limits too) and your problem will be cured. After correction, the binding may look like this:


<binding name="CustomBinding_TestService">
	<binaryMessageEncoding maxReadPoolSize="2147483647" maxWritePoolSize="2147483647" 
		maxSessionSize="2147483647">
		<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
		maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
	</binaryMessageEncoding>
	<httpTransport manualAddressing="false" maxBufferPoolSize="2147483647" 
		maxReceivedMessageSize="2147483647" allowCookies="false" authenticationScheme="Anonymous"
		bypassProxyOnLocal="false" decompressionEnabled="true" 
		hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="2147483647" 
		proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" 
		unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" />
</binding>

Please see that the max length cannot go over “2147483647” as its max integer value WCF supports. Also, it’s not required to increase the limits of other properties but it’s a good practice and also helps in preventing any future unanticipated errors.

Advertisements