How to Increase the MaxArrayLength in a WCF Component

Warning
This blog post is included for archival purposes and may contain outdated information. While it provides historical insights, we strongly recommend that you double-check the details before relying on any of the information outlined here.

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

About Author

Christian Saborio

Christian is a seasoned computer engineer with a rich career spanning collaborations with industry leaders such as Artinsoft (now Mobilize.net), Microsoft, HP, and Intel. As a technical evangelist and trainer, Christian honed his expertise in Costa Rica and Seattle, delivering impactful solutions and sharing his knowledge.

Now based in Sydney, Australia, Christian channels his passion into web development, leading a talented team to tackle diverse projects with innovation and precision. His commitment to crafting exceptional digital experiences reflects his deep-rooted enthusiasm for technology and problem-solving.

Comments

  1. Archie Seibert says:

    Thank you for this solution.

  2. Mayank says:

    You are a life saver!

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

  4. Ahmet Sekmen says:

    Thanks .

  5. 如何增加WCF连接的最大传输长度 - 谁见到过风? says:

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

  6. vinod says:

    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.

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

  8. 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?

  9. Alex says:

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

  10. Alfred says:

    Thanks! It’s a excelent post

  11. Bob says:

    Saved my @$$! Thanks so much!

  12. Brady says:

    Thanks! Fixed my problem…

  13. 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?

  14. DominionZA says:

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

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

  16. csaborio says:

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

  17. 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!

  18. Grimzon says:

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

  19. Patrick says:

    THANK YOU!!!

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

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

  22. csaborio says:

    @Patrick: Very Welcome!

  23. csaborio says:

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

  24. csaborio says:

    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 🙂

Comments are closed

Thank you for your interest. Please fill out this form to provide us with your contact information. We'll get back to you as soon as possible.