Send Large Data Sets from WCF to Silverlight

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:

  1. 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        04            07                08            09        10    11    12        15    16
  2. 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:
    0102    03        04            05            09                15            16        17    18 19    20        21            22            23                24                25                26            27        28    29 30    31    32        33        35            38            39        40    41

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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: