The web.config alteration allows a WCF service to feed large blocks of data to Silverlight. If your initial testing of a Silverlight application worked but now you are failing on large production data, this tip may be for you. Full credit goes to Syed Mehroz Alam, who diagnosed the problem and documented the solution in his blog post, “Retrieving huge amount of data from WCF service in Silverlight application.” I’m reproducing his solution here to give it a wider audience. I sweated blood over this until I stumbled across Syed’s blog.
Today, I was trying to figure out why my WCF service call always threw the generic
NotFound exception when trying to retrieve large datasets. I had all the buffer limits set to 2147483647 (
int.MaxValue) in the Silverlight service configuration file and in the WCF service configuration section under web.config.
Some analysis revealed that I was getting a System.Net.WebException:
The underlying connection was closed: The connection was closed unexpectedly.
After some research, I found that I need to set the
maxItemsInObjectGraph for dataContractSerializer to some higher value in my web.config.
If you are trapped in a similar situation, here are two steps to ensure that you can retrieve large amounts of data from a WCF service:
- Enable the Silverlight client to retrieve huge chunks of data by enabling large buffers. You need to increase buffer and message size limits for the binding inside ServiceReferences.ClientConfig with something like this:
0311 0410 0709 08 1216 15
- Enable the WCF service to send large amounts of data. You need to set the binding buffer limits as well as the DataContractSerializer’s
maxItemsInObjectGraphvalue. Here’s an extract from web.config with all the limits set to maximum:
02 0318 19 04 0517 0916 15 2029 30 21 2228 2327 24 25 26 31 32 3341 3540 38 39
That’s all it takes to send greater amounts of data from a WCF service to a Silverlight application. However, if you want to send large amounts of data from Silverlight to a WCF service, you need one more step because you can reach the maximum permitted HTTP request length (4MB by default). This again can be solved by tweaking the web.config. For example, you need something like this to allow 10MB of data: