Upload Large File of More Than 5 MB From Silverlight to Server Location Using WCF



This article is based on the article File Upload from Silverlight using WCF. So please read it before proceeding with this article.

A while back I wrote an article on uploading a File from Silverlight to a server location using WCF. I found my own article quiet useful. However, when I reread and implemented the code, I mentioned in the article, I found a few discrepancies:

  1. Code was unable to upload file with sizes in MB
  2. There was a bug in binding configuration.

In this article, I am going to rectify the above two bugs.

First modify basicHttpBinding as below:

First modify basicHttpBinding Silverlight

Configure Endpoint as below:

Configure Endpoint in silverlight

So far everything is fine and if you run the code, you will be able to upload a file of a maximum size of 3MB. I tested it and I was able to upload a document file of 2.75 MB in size.

Now the question becomes, what if we want to upload a file of about 10 MB in size?

For that we need to reconfigure the things in the configuration file:
  1. Configure service behavior maximum number of items to serialize or deserialize.
  2. Configure HTTP Request length limit.

You need to explicitly configure the behavior of the service to the maximum number of items to serialize or deserialize. This can be configured either in code or in a configuration file.
 
MaxItemsInObjectGraph property of DataContractSerilalizer determines the number of items that can be serizlized or deserizlized.

MaxItemsInObjectGraph silverlight

The Http Request limit can be configured as below:

Http Request Silverlight

After completing the preceding changes to the configuration file the service would look like below:

<?xml version="1.0"?>
<
configuration>
<
system.web>
<
compilation debug="true" targetFramework="4.0" />
<
httpRuntime maxRequestLength="10240" />
</
system.web>
<
system.serviceModel>
<
bindings>
<
basicHttpBinding>
<
binding name="ServicesBinding"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<
readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</
binding>
</
basicHttpBinding>
</
bindings>
<
services>
<
service name="ServiceData.Service1" behaviorConfiguration="ServiceData.Service1Behavior">
<
endpoint address="" binding="basicHttpBinding" contract="ServiceData.IService1" bindingConfiguration ="ServicesBinding" >
<
identity>
<
dns value="localhost"/>
</
identity>
</
endpoint>
<
endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</
service>
</
services>
<
behaviors>
<
serviceBehaviors>
<
behavior name ="ServiceData.Service1Behavior">
<
serviceMetadata httpGetEnabled="true"/>
<
serviceDebug includeExceptionDetailInFaults="false"/>
<
dataContractSerializer maxItemsInObjectGraph ="2147483647"/>
</
behavior>
</
serviceBehaviors>
</
behaviors>
<
serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</
system.serviceModel>
<
system.webServer>
<
modules runAllManagedModulesForAllRequests="true"/>
</
system.webServer>
</
configuration>


Now you should able to upload a large file such as 10 MB in size from a Silverlight client to the server location using WCF.

Up Next
    Ebook Download
    View all
    Learn
    View all