How to Increase the MaxArrayLength in a WCF Component
I was doing some benchmarking for a client to see if Interop calls from a WCF service would hinder performance and defy the whole purpose. I was using variable byte arrays to send data to build a performance graph of data vs. time.
My efforts were quickly shut down by the fact that by default (to avoid exploits) WCF cannot receive data in an array from a client whose length is bigger than 16K:
“The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation ‘XXX’. 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. Line 1, position 39987.”
Like the message says, you can change these values in the client’s app.config file, but that is not taking you anywhere. There are various scenarios for this issue:
- WCF Component Hosted in IIS
- Self Hosted WCF Component
To allow bigger arrays, you need to change 2 config files:
- The client’s app.config
- The Server’s config file
Let’s start with the client’s config file:
Find the readerQuotas element and change the values of the following attributes to 2147483647: maxDepth, maxStringContentLength, maxArrayLength, maxBytesPerRead, maxNameTableCharCount (this may be overkill as probably maxArrayLength does the trick, but I did not want to take any chances):
In order to fix the server’s config file, you need to edit the web.config file if the service hosted on IIS *OR* the app.config file of the self hosted web service. First, you want to make sure that you define a a new binding:
Once you have defined this binding, you need to add it to your services endpoint definitions (line 3)
Windows LiveWriter rocks!