ScorpioTek Solutions
07-filler-left Information about ScorpioTek Services Offered by ScorpioTek Training Courses Offered by ScorpioTek Technology Blog by ScorpioTek Contact us for more information on training/services 08-filler-right
06-message

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"

Windows LiveWriter rocks!

Source:

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

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

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

  4. csaborio Says:

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

  5. Patrick Says:

    THANK YOU!!!

  6. csaborio Says:

    @Patrick: Very Welcome!

  7. Grimzon Says:

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

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

  9. csaborio Says:

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

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

  11. DominionZA Says:

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

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

  13. Brady Says:

    Thanks! Fixed my problem…

  14. Bob Says:

    Saved my @$$! Thanks so much!

  15. Alfred Says:

    Thanks! It’s a excelent post

  16. Alex Says:

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

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

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

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

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

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

  21. Ahmet Sekmen Says:

    Thanks .

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

  23. Mayank Says:

    You are a life saver!