Tuesday, October 21, 2008

WCF overview

WCF(Web Communication Foundation) technology is using multiple layer in configuring web service. The three main layers are

1. Address - to specify where is your web service is

2. Binding - to specify how we are going to connect to the web service. For example, using http or https or tcp. Whether we want to use X509Certificate or we want to use Windows Authentication or NTLM or Keboros. And what's is the file upload/download behavior etc

3. Contracts - This is where you write functions for web services. This is the business logic area where you write all your coding based on business logic.

People refers these three layers as A(Address), B(Binding) and C(Contracts) of WCF.

The Address and Binding can be specify either in the coding or in web.config file. The best practise is to specify the Address and Binding in web.config file. The reason is simple. The address and binding can be keep changing based on IT infrastructure requirement and configuration. If we specify the address and binding, when we change the web service hosting environment we have to change the configuration inside the coding and re-compile and re-deploy which is actually quite tedious. If you configure everything in web.config, the moment you need to make changes to the web service address, or certificate or authentication all you need to do is just open up web.config file and make the changes. There is no need to re-compile the code at all.

Other advantage of WCF is it's interoperability. WCF can communicate better with other Web Service technologies like Java, Message Queue, Remote Hosting, COM + etc.

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.