How to Increase the MaxArrayLength in a WCF Component

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):

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

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:

   1: <configuration>
   2:   <system.serviceModel>
   3:     <bindings>
   4:       <basicHttpBinding>
   5:         <binding name="myBindingForBigArrays"
   6:             maxReceivedMessageSize="2147483647">
   7:           <readerQuotas
   8:               maxDepth="64"
   9:               maxStringContentLength="2147483647"
  10:               maxArrayLength="2147483647"
  11:               maxBytesPerRead="4096"
  12:               maxNameTableCharCount="16384"/>
  13:         </binding>
  14:       </basicHttpBinding>
  15:     </bindings>

Once you have defined this binding, you need to add it to your services endpoint definitions (line 3)

   1: <services>
   2:   <service behaviorConfiguration="NewBehavior" name="XXX">
   3:     <endpoint bindingConfiguration="myBindingForBigArrays" address="basic" binding="basicHttpBinding"


24 responses to “How to Increase the MaxArrayLength in a WCF Component”

  1. bad wcf developer says:

    thanks. i’ve read at least a dozen articles tonight on how to do this but my config problem only hit home when reading your blog.

  2. chris says:

    Thanks – came across this pretty quickly and it helped me clear up my problem within an hour. Saved me probably at least an hour or two so thanks for posting

  3. Patrick says:

    THANK YOU!!!

  4. Grimzon says:

    Thanx a bunch mate, worked fine for me. My problem was I didn’t have the in the server config.

  5. LnddMiles says:

    Pretty cool post. I just stumbled upon your blog and wanted to say
    that I have really liked reading your blog posts. Anyway
    I’ll be subscribing to your blog and I hope you post again soon!

  6. csaborio says:

    @LnddMiles: Thanks for your comments! 🙂 We’re glad you enjoy our posts, thanks for subscribing, we’ll have some new material shortly.

  7. D@ says:

    thank you thank you thank you!!!!!!!!!!!

    been reading a lot searching for the answer for a couple of days… tood me less than 3 mintues after reading this…..

  8. DominionZA says:

    HUGE thanks man. This got me sorted in 2 mins flat!!

  9. Jep says:

    I hate to spoil the good mood here but that did not help with in case. Did all the steps but I still get the same error message.

    I wonder if i need to specify in the client app.config to use the new binding ?

    Any other idea?

  10. Brady says:

    Thanks! Fixed my problem…

  11. Bob says:

    Saved my @$$! Thanks so much!

  12. Alfred says:

    Thanks! It’s a excelent post

  13. Alex says:

    thank you very much. This post is very helpful!!! bye

  14. Shan says:

    Hi there thanks for the nice post. I’m just getting started with Silverlight & WCF so this was a challenging issue to overcome. I’m using Silverlight 4 with Visual Web Developer 2010 Express. In VWD when you add a new “Silverlight-enabled WCF Service” item, it adds custom binding by default into the web service web.config. I wasn’t able to configure the custom binding to support large requests – maybe I missed something – so I reconfigured the binding to use basicHttpBinding as indicated in your sample, and my web service requests are finally working properly.

    I’m not very familiar with WCF so I don’t know if I would be able to configure the out of the box custom binding to support this or not, has anyone tried?

  15. Ayaz says:

    Great post but I need a little more. I modified my client App.config to what you said. I also modified my server config to add the tag. Then you mentioned “Once you have defined this binding, you need to add it to your services endpoint definitions (line 3)” and I am not sure what needs to happen here. I am including my server webconfig without the last step. Can you please help by telling me where to put the endpoint information. Thanks in advance.

    Server web.config

  16. vinod says:

    Thank you.
    I tried all changes.
    The only thing that I found in your solution, that I missed was the bindingConfiguration parameter in endpoint element.
    Once that is set, my application worked perfectly fine, for large strings.

  17. […] 这篇文章写的很详细:How to Increase the MaxArrayLength in a WCF Component […]

  18. Ahmet Sekmen says:

    Thanks .

  19. Hugo says:

    It’s nice when someone put how to do things for noobs like me, I resolve the problem reading your article after hours of frustration

  20. Mayank says:

    You are a life saver!

  21. Archie Seibert says:

    Thank you for this solution.

Leave a Reply

Your email address will not be published. Required fields are marked *