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:
01<system.serviceModel>02
03 1104 1007 0908 12 1615 - Enable the WCF service to send large amounts of data. You need to set the binding buffer limits as well as the DataContractSerializer’s
maxItemsInObjectGraph
value. Here’s an extract from web.config with all the limits set to maximum:01
02 03 18 1904 05 1709 1615 20 29 3021 22 2823 2724 25 26 31 32 33 4135 4038 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:
1