RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Send Large Data Sets from WCF to Silverlight

Web.config alterations allow a WCF service to feed data to Silverlight applications, but may they may fail for large production data. Find out how to work with large data sets.


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:
    02    <bindings>
    03        <basicHttpBinding>
    04            <binding name="BasicHttpBinding_SilverlightWCFService"
    05                maxBufferSize="2147483647"
    06                maxReceivedMessageSize="2147483647">
    07                <security mode="None" />
    08            </binding>
    09        </basicHttpBinding>
    10    </bindings>
    11    <client>
    12        <endpoint address="http://localhost:1252/SilverlightWCFService.svc"
    13            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SilverlightWCFService"
    14            contract="SilverlightWCFService.SilverlightWCFService" name="BasicHttpBinding_SilverlightWCFService" />
    15    </client>
  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:
    02    <bindings>
    03        <basicHttpBinding>
    04            <!-- Create a custom binding for our service to enable sending large amount of data -->
    05            <binding name="MyBasicHttpBinding"
    06                maxBufferPoolSize="2147483647"
    07                maxReceivedMessageSize="2147483647"
    08                maxBufferSize="2147483647">
    09                <readerQuotas
    10                    maxArrayLength="2147483647"
    11                    maxBytesPerRead="2147483647"
    12                    maxDepth="2147483647"
    13                    maxNameTableCharCount="2147483647"
    14                    maxStringContentLength="2147483647" />
    15            </binding>
    16        </basicHttpBinding>
    17    </bindings>
    19    <behaviors>
    20        <serviceBehaviors>
    21            <!-- Enable the serializer to serialize greater number of records -->
    22            <behavior name="SilverlightWCFLargeDataApplication.Web.SilverlightWCFServiceBehavior">
    23                <serviceMetadata httpGetEnabled="true"/>
    24                <serviceDebug includeExceptionDetailInFaults="false"/>
    25                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    26            </behavior>
    27        </serviceBehaviors>
    28    </behaviors>
    30    <serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>
    31    <services>
    32        <!-- Bind the WCF service to our custom binding -->
    33        <service behaviorConfiguration="SilverlightWCFLargeDataApplication.Web.SilverlightWCFServiceBehavior"
    34                name="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService">
    35            <endpoint address="" binding="basicHttpBinding"
    36                bindingConfiguration="MyBasicHttpBinding"
    37                contract="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService"/>
    38            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    39        </service>
    40    </services>

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<httpRuntime maxRequestLength="10240" />

Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date