Long back I wrote post on uploading a File from Silverlight to server location using WCF. I found my own post quiet useful. However, when I reread and implemented the code, I mentioned in the post, I found few discrepancies
- Code was unable to upload file with sizes in MB
- There was a bug in binding configuration
In this post, I am going to rectify above two bugs.
First modify basicHttpBinding as below,
Configure Endpoint as below,
So far everything is fine and if you run the code, you would able to upload a file maximum of the size 3MB. I tested and I was able to upload document file of size 2.75 MB.
Now question comes, what if we want to upload a file of the size around 10 MB?
For that we need to reconfigure the things in configuration file
- Configure service behavior maximum number of items to serialize or deserialize.
- Configure HTTP Request length limit.
You need to explicitly configure the behavior of service to the maximum number of items to serialize or deserialize. This can be configured either in code or in configuration file.
MaxItemsInObjectGraph property of DataContractSerilalizer determines number of items can be serizlized or deserizlized.
Http Request limit can be configured as below
After completing above changes configuration file at 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 as of size 10 MB from Silverlight client to the server location using WCF
Follow @debugmode_ **************