WCF And Large Amounts Of Data
Posted by Davy Brion on September 14th, 2008
If you’re using my Request/Response service layer, or any other WCF service that might require sending large amounts of data over the wire, you quickly bump into some limits that WCF enforces by default. Among the billions of configuration options for WCF, there are luckily some options that allow you to easily send large amounts of data from a service to a client.
I typically use the following options:
For my binding configuration i usually set the maxReceivedMessageSize, maxStringContentLength and the maxArrayLength properties to their maximum values:
<bindings>
<netTcpBinding>
<binding name="MyTcpBinding" maxReceivedMessageSize="2147483647" receiveTimeout="00:30" sendTimeout="00:30">
<readerQuotas maxStringContentLength="8192" maxArrayLength="20971520" />
</binding>
</netTcpBinding>
</bindings>
This example shows these settings for the netTcpBinding… i’ve also used them with the wsHttpBinding. Not sure how well it works with other bindings though.
I also set the maxItemsInObjectGraph setting of the DataContractSerializer to make sure i don’t hit the default limit if i have to send a large object graph over the wire:
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
You have to apply these settings both server and client side to get it working properly and you need to refer to these settings in your service and endpoint settings:
<service name="Brion.Library.ServerSide.WCF.WcfRequestProcessor" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost/RequestProcessor"/>
</baseAddresses>
</host>
<endpoint contract="Brion.Library.Common.WCF.IWcfRequestProcessor" binding="netTcpBinding"
bindingConfiguration="MyTcpBinding" />
</service>
<client>
<endpoint address="net.tcp://localhost/RequestProcessor" binding="netTcpBinding"
name="IRequestProcessor" bindingConfiguration="MyTcpBinding" behaviorConfiguration="MyBehavior"
contract="Brion.Library.Common.WCF.IWcfRequestProcessor" />
</client>
Now, i don’t recommend sending such large amounts of data through WCF services… but in the case of using my Request/Response service layer, the amount of data you’re sending over the wire pretty much depends on which kind of requests (and how many of them) you’re batching so i think it’s better to make sure that at least the configuration allows for it. So obviously, it’s best to keep an eye on the size of your messages to make sure you’re not doing anything crazy. Being able to send your entire database over the wire doesn’t mean it’s a good idea to actually do so

September 14th, 2008 at 3:38 pm
[...] WCF and Large Amounts of Data (Davy Brion) [...]
June 3rd, 2010 at 11:49 am
Thanks a lot
The info regarding maxItemsInObjectGraph solved a problem that i have spent at lot of time pursuing until now!
July 21st, 2010 at 4:12 pm
How do I send large amounts of data TO the WCF? I get a Bad request exception when I try to do so.
July 21st, 2010 at 7:23 pm
@Hari
if you’re hosting through IIS, make sure you enable large uploads in your web.config:
http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx