Sunday, October 19, 2008

File streaming limitation on WCF

When I started testing out WCF for web services using VS 2008 the first issue I encounter is when I try to stream the file from my client pc to web services. By default WCF allow ridiculously small file size to stream through internet. The way to work around from this problem is to edit the web.config file in your WCF project. Below is the example

You need to define the binding configuration first

<bindings>
<basichttpbinding>
<binding name="uploadBinding" maxreceivedmessagesize="9223372036854775807" transfermode="Streamed" sendtimeout="00:10:00">
</binding>
</basichttpbinding>
</bindings>

And at endpoint configuration you need to reuse the binding name (in this example uploadBinding) so that endpoint will know what is the size WCF is allowing to receive. I put the maximum digit for maxReceivedMessageSize in the example. You can actually define a lesser limit if your web service is not intended to process a very large file

<endpoint address="" binding="basicHttpBinding" bindingconfiguration="uploadBinding" contract="Demo.WCFService.IService1">

The next issue in streaming file is when you upload a XML file you can run into the error message similar to this

The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 2302.'. Please see InnerException for more details.

It is because the MaxArrayLength allow in web.config by default is just 16384 bytes which is 16 kb. I wonder how many xml file we use for storing of data could be below 16 kb. Really!.. This is so amazing!!

You can resolve this issue by amending web.config file using Tool->WCF Configuration Tool from VS 2008 Editor, loading the existing web.config file and change the parameter values accordingly in ReaderQuotas section.

This should resolve most of the issues with uploading streaming content or downloading them.

Just take note of the two other facts below

1. Only the following binding schemes including BasicHttpBing, WebHttpBinding, NetTcpBinding and NetNamedPipeBinding are allowed to transfer stream data.
2. Transferred data must be serializable Stream data or MemoryStream data.

If the data is not serializable you will encounter error.

No comments: