How to Increase the MaxArrayLength in a WCF Component

By , , With 24 Comments

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"

Sources:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/75065538-eed4-48cd-8252-1ff3e795bcfb

Comments (24)

  1. bad wcf developer February 21, 2009

    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. csaborio February 21, 2009

    Glad to hear you solved it. I too spent many hours reading how to fix this and none of the blogs offered a step-by-step guide; hence I wrote this. The fact that it helped one person makes it all worthwhile :)

  3. chris April 9, 2009

    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

  4. csaborio April 10, 2009

    @chris – thanks for posting, cool to see that this post helped you as well.

  5. Patrick May 17, 2009

    THANK YOU!!!

  6. csaborio May 17, 2009

    @Patrick: Very Welcome!

  7. Grimzon July 20, 2009

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

  8. LnddMiles July 22, 2009

    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!

  9. csaborio July 22, 2009

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

  10. D@ August 19, 2009

    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…..

  11. DominionZA December 22, 2009

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

  12. Jep December 23, 2009

    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?

  13. Brady March 4, 2010

    Thanks! Fixed my problem…

  14. Bob April 5, 2010

    Saved my @$$! Thanks so much!

  15. Alfred April 8, 2010

    Thanks! It’s a excelent post

  16. Alex May 10, 2010

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

  17. Shan May 10, 2010

    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?

  18. Ayaz August 20, 2010

    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

  19. vinod January 7, 2011

    Hi,
    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.

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

  21. Ahmet Sekmen April 24, 2011

    Thanks .

  22. Hugo September 20, 2011

    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

  23. Mayank December 5, 2011

    You are a life saver!

  24. Archie Seibert September 12, 2014

    Thank you for this solution.

Leave a Reply