Today, I was trying to figure out why my WCF service call was always throwing the generic NotFound exception when trying to retrieve large datasets. I had all the buffer limits set to 2147483647 (int.MaxValue) at Silverlight service configuration file as well as WCF service configuration section under web.config. Some more analysis revealed that I was getting a System.Net.WebException, saying: 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.
So, if you are trapped in a similar situation, here are two steps to ensure that you can retrieve large amount of data from WCF service:
1. Enable 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:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_SilverlightWCFService"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1252/SilverlightWCFService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SilverlightWCFService"
contract="SilverlightWCFService.SilverlightWCFService" name="BasicHttpBinding_SilverlightWCFService" />
</client>
</system.serviceModel>
2. Enable WCF service to send large amount of data. You need to set the binding buffer limits as well as tje DataContractSerializer’s maxItemsInObjectGraph value. Here’s an extract from web.config with all the limits set to maximum:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<!-- Create a custom binding for our service to enable sending large amount of data -->
<binding name="MyBasicHttpBinding"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<!-- Enable the serializer to serialize greater number of records -->
<behavior name="SilverlightWCFLargeDataApplication.Web.SilverlightWCFServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>
<services>
<!-- Bind the WCF service to our custom binding -->
<service behaviorConfiguration="SilverlightWCFLargeDataApplication.Web.SilverlightWCFServiceBehavior"
name="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="MyBasicHttpBinding"
contract="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
That was all related to sending greater amount of data from WCF service to Silverlight application. However, if you want to send large amount of data from Silverlight to WCF service, you need one more step as you can reach the maximum permitted http request length limit (this is 4MB by default). This again can be solved by tweaking the web.config. E.g. to allow 10MB data, you need something like:
<httpRuntime maxRequestLength="10240" />
That’s all. Let’s enjoy developing LOB applications with this great platform.
February 4, 2009 at 11:37 PM
[...] a custom binding allows you to configure buffers and quotas as described in this post. The definition of this custom binding will look something [...]
September 7, 2009 at 4:29 AM
Thanks a 1000! you saved 2 hrs of debug time to fig this out, Thanks again..
October 29, 2009 at 1:26 AM
This solved my problem. Thanks!
November 19, 2009 at 11:41 AM
Thanks a lot…This solved my problem also..
November 20, 2009 at 12:10 AM
Thank you sir! I’ve been trying many things to get around these timeouts.
November 25, 2009 at 2:09 PM
It seems to solve one part of my problem.
Receiving large data from WCF is working great with this configuration … but how about Re-sending large data from Silverlight to WCF ?
With your solution, I’m able to retrieve a big ObservableCollection in my Silverlight Application, but after I can’t send them back to the wcf
Do you have any ideas ?
November 25, 2009 at 2:19 PM
Thanks a lot………………………………solved my problem.
November 25, 2009 at 5:45 PM
@Christophe
In order to send large amount of data from Silverlight to WCF service, you typically reach the maximum permitted request length limit which is 4MB by default. This can be solved by modifying your web.config, e.g. to allow 10MB data, you need something like:
Here’s the related MSDN document: http runtime
December 28, 2009 at 10:01 AM
Hi this works for me less than 61 mb files but for more than 61 mb it shows the NOT FOUND exception
December 31, 2009 at 3:07 PM
@Muthu,
Can you provide some more details.
Is it a file transfer from your Silverlight client to the WCF service or it is vice versa?
Also, if you are uploading files from the Silverlight client to the server, are you using a WCF service method or an ASHX handler? Typically, I would go for the ASHX handler instead of sending huge binary data through WCF.
February 24, 2010 at 9:21 PM
Great article. Although @Christophe’s suggestion about the httpRuntime didn’t work for me. It caused no data to return.
I was getting data limits of just over 1,000 records in my DataGrid. Now with these changes to the web.config in my Data Project, I get all the records.
Thanks,
Bryant
February 26, 2010 at 5:40 PM
the problem is resolved as far as you data is under 2MB of size but as data increased from 2MB then the service again throw the exception. how can we fetch the data from server to silverlight client greater then 2MB
February 27, 2010 at 11:58 AM
Hey,
Great Article.
Solved My problem.
Thanks a Lot
Wish you luck
February 27, 2010 at 9:00 PM
Saqib,
That’s strange. Are you sure you didn’t miss any of the configurations described in this post.
Also, to further investigate, I recommend you to use Fiddler to see what error you are getting on the wire. Also, another way of debugging into the problem would be to create another application (could be web or desktop application) and try to consume your Silverlight WCF service from that application.
Finally, you can always ask your question in the Silverlight forums. There are a lot of active community members to help you out.
Hope that helps.
March 1, 2010 at 11:50 PM
I’m encountering a similar issue; however, changing my web.config does not fix the problem.
I didn’t follow step 1 because I don’t seem to have a ServiceReferences.ClientConfig file in my project. Do I need to create one? Where does it live in the directory structure? Do I need to modify the project in any way to make it pick it up?
I’m using a Silverlight Navigation Project with WCF RIA services. Everything worked fine in the July 09 RIA but this is now occuring only for this one query which is returning a lot of data.
March 2, 2010 at 1:46 PM
@Michael,
This post is related to the typical WCF services that run in ASP.NET compatibility mode. I will do some research for the same stuff using WCF RIA Services over the weekend and will update the post. Thanks for pointing out.
March 5, 2010 at 10:42 PM
This is a life saver … thanks!
April 28, 2010 at 7:19 PM
GREAT SOLUTION!!! my asp client app was trying to upload large binary files to a wcf entity service, but would simply ignore large upload commands.
I made the suggested corrections in seconds and viola!! WORKS PERFECTLY!!!!
May 13, 2010 at 3:41 PM
Thank you very much
a great post ^_^
May 25, 2010 at 10:40 PM
I have tried all of this, any ideas?? TIA
Web.config:
ServiceReferences.ClientConfig:
June 10, 2010 at 11:55 PM
thanks so much.
this is the step I was missing!
June 10, 2010 at 11:57 PM
apparently the blog engine didn’t like the format of my last message. The part that was helpful to me was setting the “HttpRuntime maxMessageLength” value.
Thanks for the post.
June 11, 2010 at 11:38 AM
Thanks, Jeremy.
To post xml sourcecode, you need to encapsulate it like this:
[sourcecode language="xml"]your code here
[/sourcecode]
The list of supported languages can be found here.
July 1, 2010 at 3:55 PM
Apparently SL4 does not use ClientConfig by default, and there isn’t one hidden in the XAP.
How best can I implement the Client Side solution? (Server Side works great!)
Thanks
Pat NH USA
July 4, 2010 at 8:26 PM
Hi Pat,
The
ClientConfigis not generated in case of WCF RIA services, so I assume you are referring to RIA services. I created a new WCF RIA service with Silverilght 4 RTM and successfully sent 500,000K rows to the client and it only required the following addition to myweb.config:<system.serviceModel> <behaviors> <serviceBehaviors > <behavior name=""> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>If your problem still perists, can you upload a small app somewhere?
July 19, 2010 at 1:29 PM
Thank you very much, this solved my problem!!
September 9, 2010 at 1:47 AM
@Syed Mehroz Alam
Perfect. Just perfect! I used your suggestion, and it worked at once! It’s probably ideal for those with a DOS-attack, but tweaking comes later.
September 14, 2010 at 7:59 PM
Thanks Syed!
this line
did the trick
September 22, 2010 at 9:58 AM
Syed, just wanted to say thanks. Amazing article. You saved me a LOT of wasted hours.
September 23, 2010 at 6:06 PM
Great Posting, It solved My Problem
October 10, 2010 at 4:25 PM
Sill can’t get it to work.
Could someone upload a working example.
Tried directly coping and pasting(changing the ports etc) this example then I get a “html document does not contain a Web Service Information”
November 29, 2010 at 3:44 PM
solve my problem.thank you..
December 2, 2010 at 3:24 PM
We had a presentation of our application which uses Silverlight client that talks to WCF service on the server side. We have tried this application in several network environments
previously but once we hosted it on customer’s IIS it started showing some serious problems. After several hours we came across this solution and it worked. Real life saver, Thank’s!
December 8, 2010 at 2:46 PM
thank alot. since pas two days …. my time wasted on this item but your artivle solved my problem.
good luck
December 10, 2010 at 5:48 PM
Thanks a lot!! Saved my Project.
January 13, 2011 at 10:43 AM
I’m very happy to solve my problem.
thank you.
thanks.
February 11, 2011 at 10:11 AM
Tons of thanks.
February 20, 2011 at 2:24 AM
Fantastic, thanks. A real life saver.
March 4, 2011 at 2:05 PM
hi, i tried it but i donno if i am doing something wrong. can u check it plz
<?xml version="1.0"?> <configuration> <system.web> <httpRuntime maxRequestLength="10240"/> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <!-- Create a custom binding for our service to enable sending large amount of data --> <binding name="MyBasicHttpBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" /> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false"/> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> <behavior name="NewBehavior0"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="false"/> <services> <service behaviorConfiguration="NewBehavior0" name="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService"> <endpoint address="http://localhost/WebTrackerService/MainService.svc" binding= "basicHttpBinding" bindingConfiguration="MyBasicHttpBinding" contract="SilverlightWCFLargeDataApplication.Web.SilverlightWCFService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> <appSettings> <add key="ConnectionString" value="Data Source=.;Initial Catalog=x;User ID=x;Password=x" /> </appSettings> </configuration>March 17, 2011 at 6:13 PM
The line aspNetCompatibilityEnabled must be set to true not false !!!
March 18, 2011 at 9:03 PM
[...] I found this excellent post: http://smehrozalam.wordpress.com/2009/01/29/retrieving-huge-amount-of-data-from-wcf-service-in-silve… and I was the key to solve a problem I had with a WCF [...]
March 30, 2011 at 9:06 PM
This helped me out alot thanks
April 10, 2011 at 1:58 AM
This worked like a charm. Thanks!
May 20, 2011 at 10:34 AM
Super thanks, that worked wonderfully. After spending almost 8 hrs on this, finally found your page which resolved the problem.
Thanks
May 20, 2011 at 2:37 PM
Hey, It worked for me.
Thanks a lot for sharing.
May 28, 2011 at 10:08 PM
Post is not there. i m getting page not found error.
May 31, 2011 at 9:13 AM
I hosted WCF Service on IIS, then cann’t upload file from Silverlight client to the WCF service ,can u check it plz
Silverlight client
WCF service
May 31, 2011 at 9:14 AM
May 31, 2011 at 9:23 AM
@”
“
May 31, 2011 at 10:45 AM
if host WCF Service on IIS 7,must configuration “maxAllowedContentLength” in web.config
June 6, 2011 at 9:16 PM
Thanks a lot! you save my day!
June 21, 2011 at 6:58 AM
When I developed Silverlight WCF application, I failed to change the Web.Config setting to retrieve huge amount of data.
Any one can give me some tips?
With Visual Studio 2010, Make a new project “BusinessApplication1″ based on “Silverlight” and “Silverlight Business Application” template.
Within the “BusinessApplication1.Web” (the server side), you add new item “Service1.svc” based on template “Web” and “WCF Service”.
The interface is as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace BusinessApplication1.Web
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void DoWork();
}
}
The implementation is as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace BusinessApplication1.Web
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public void DoWork()
{
}
}
}
At the silverlight client side “BusinessApplication1″ , you can then discover the WCF services and generate the proxy classess and meta data for “Web Method” “void DoWork()”.
The “client” “endpoint” and “binding” configurations have been automatically added into client configuation file “ServiceReferences.ClientConfig”, which will be added into the .xap file.
Actually it is unnecessary to add the “service” “endpoint” and “binding” configuration within the Web.Config file in the Server project “BusinessApplication1.Web”. If you DO put those “service” and “endpoint” configurations with Web.Config file in the Server project “BusinessApplication1.Web”, it has NO effect.
How could we change the settings of those ASP.NET hosted WCF Services if we changed the configurations within the Web.Config has NO effect?
Whe we deploy those or publish those server projects into IIS, how could we modify the “Web.Config” and how we change the host address and port number?
Cheers,
June 21, 2011 at 7:07 AM
Sorry for not mentioning:
I am using “Silverlight 4.0″ and “Visual Studio 2010″.
Ria toolkits have been installed. But for this project the template is “Silverlight Business Application”.
We can actually add “Domain Service” rather than “WCF Service”. But the “Domain Service” failed to support complex “DataContract” etc..
Thanks in advance..
July 19, 2011 at 11:45 AM
What about tranferring 1 GB data without using streaming ?
September 23, 2011 at 3:54 PM
[...] Retrieving huge amount of data from WCF service in Silverlight application Like this:НравитсяБудьте первым, кому понравился этот . Опубликовано в ASP.NET, C#, Silverlight, WCF. Оставьте комментарий » [...]
September 23, 2011 at 7:18 PM
Hi,
i am not able to send large amount of data from Sl4 to WCf my web.config file is as below.
Please help me out what i am doing wrong
September 23, 2011 at 7:19 PM
October 17, 2011 at 2:39 PM
This worked wonders! I had been at this for a week now. Thanks for the post!
November 14, 2011 at 7:49 PM
I tried everything about and no matter what config settings I set I cannot send large data from Silverlight to WCF.
I have tried this:
No matter what I do I get a the “Not Found” exception.
November 22, 2011 at 3:53 PM
I tried everything about and I cannot send large data from Silverlight to WCF ria services.
November 24, 2011 at 11:04 PM
Hi,
I’m new to all this Silverlight and Services stuff. Trying to teach myself. I have written an app that returns datat from a datebase, however my LoadOp returns 0 rows after the parameters into my query make the returned collection exceed 2730 row. I am using a SilverLight Bussiness Application with RIA Services.
I have tried what you ave suggested, but it seems to no avail. I can’t seem to pull more that 2730 rows of data from my database. I can run the same query in TOAD and i know there are 4500+ rows to return. Can you offer any suggestions of anything I have done wrong, or anything else to check?
Below is my Web.Config.
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="system.serviceModel"> <section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" /> </sectionGroup> </configSections> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="MES_SteriDataModelConnection" connectionString="metadata=res://*/Models.MES_SteriDataModel.csdl|res://*/Models.MES_SteriDataModel.ssdl|res://*/Models.MES_SteriDataModel.msl;provider=Oracle.DataAccess.Client;provider connection string="DATA SOURCE=xxxxxxxx;PASSWORD=xxxxxxx;PERSIST SECURITY INFO=True;USER ID=xxxxxx"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.web> <httpModules> <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </httpModules> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </assemblies> </compilation> <globalization culture="auto" uiCulture="auto" /> <authentication mode="Forms"> <forms name=".SterilizerTrendsAnalysis2GT_ASPXAUTH" timeout="2880" /> </authentication> <membership> <providers> <clear /> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <roleManager enabled="true"> <providers> <clear /> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> <profile> <providers> <clear /> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /> </providers> <properties> <add name="FriendlyName" /> </properties> </profile> <httpRuntime executionTimeout="600" maxRequestLength="40960" /> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true"> <add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </modules> </system.webServer> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="MyBasicHttpBinding" hostNameComparisonMode="StrongWildcard" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" transferMode="Buffered" messageEncoding="Text" textEncoding="utf-8" bypassProxyOnLocal="false" useDefaultWebProxy="true" > <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" /> <security mode="None" /> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <!-- Enable the serializer to serialize greater number of records --> <behavior name="SterilizerTrendsAnalysis2GT.Web.SilverlightWCFServiceBehavior"> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> <services> <!-- Bind the WCF service to our custom binding --> <service behaviorConfiguration="SterilizerTrendsAnalysis2GT.Web.SilverlightWCFServiceBehavior" name="SterilizerTrendsAnalysis2GT.Web.SilverlightWCFService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyBasicHttpBinding" contract="SterilizerTrendsAnalysis2GT.Web.SilverlightWCFService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> </configuration>November 28, 2011 at 10:58 PM
Thanks alot..you saved my time..
Once again thanks
Foram
December 2, 2011 at 9:52 PM
Thanks for making such a clear post explaining this.
February 1, 2012 at 7:24 PM
THANKS A LOT!!! I guess it saved me hours of searching!
February 14, 2012 at 12:01 PM
Thanks a lot it saved me i was googling since two days for solution but certainly your code has helped me a lot.
February 15, 2012 at 11:12 AM
hello ,
i have problem is that i send data on WCF services through Iphone and android then show Bad request .i have done all process those you written above .if i was sending huge amount of data send on server then show error so .please help me ..
February 18, 2012 at 3:54 AM
I’m pulling 3400+ records into my Silverlight app via WCF RIA. The solution listed here did not work for me by itself. However, after I’ve modified my service behavior in the following way (according to this post: http://forums.silverlight.net/p/200978/624616.aspx/1?p=True&t=634650978720627933), everything worked just fine:
April 18, 2012 at 3:31 AM
Thanks a Lot! I really mean it
August 21, 2012 at 6:57 PM
Hi i have Pulled 86400 records from WCF but i was not able to bind it to the data grid….i have assigned the whole collection of data to the itemsource of datagrid.But due to this the screens hangs up and after some time its stops responding and ultimately crashes my internet explorer.Please anyone can help….
September 27, 2012 at 6:13 AM
Howdy! This article could not be written any better!
Going through this post reminds me of my previous roommate!
He continually kept talking about this. I will forward this post to
him. Pretty sure he’s going to have a good read. I appreciate you for sharing!
September 27, 2012 at 3:52 PM
Thanks a lot…..It works…
December 3, 2012 at 1:56 PM
Thanks a lot for the nice information solve my problem
December 6, 2012 at 2:04 PM
i need to pull more than 10,000 of items from service to client but its not possible i can send till 5,000.
Plz can you help on this soon