I recently worked on a project where I developed an WCF web services that is exposed via  https and net.tcp. The web services does all the database calls and business logic. Here are some lessons or tips for developing in WCF with net.tcp.

When I use the net.tcp channel to communicate to the web service I often get the following error:

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:01:00'.

This is the default error message that you will get from WCF if something went wrong but the WCF channel does not know exactly what error occurred.

What to do when you receive this message.

  1. Increase the ReceiveTimeout and SendTimeout on the binding that is used by your web service to 5 min. The other timeout properties default values are 99.9% always correct. The 5 min is very big and will usually eliminate actual timeout issues.
  2. The usual suspects for the error is MaxBufferSize, MaxBufferPoolSize, MaxReceivedMessageSize, MaxArrayLength default size (65536) is to small. Increase them by adding a 0 at the end (655360) to see if the error disappear. Then by small increments reduce the size to find the correct value.
  3. If your web service method returns an big message or generic array or list with a big items count and big object graph you have to increase the value of the property MaxItemsInObjectGraph in the dataContractSerializer element. You need to create a Service Behaviour (Service side) and Endpoint Behaviour (Client side) with the dataContractSerializer element. You will then see the MaxItemsInObjectGraph property.
  4. To increase your communication between service and client you can switch the TransferMode in the binding from Buffered to Streamed.
  5. When you call the web service from an .Net client please avoid calling your code inside a using statement. Rather use try-finally where you can call close on the client object and if the communication channel faulted you can call abort on the client object.

These lessons will hopefully make your net.tcp development experience easier. If you have any further tips or lessons please leave a comment.

Cheerio!

Here is my talk that I presented at Devs4Devs. Thanks to Microsoft for the opportunity and thanks to the guys who attended.

At the bottom is the links to my presentation and sample code. Before you can use the sample you need to setup Windows Server AppFabric Cache and use the AdventureWorks Database.

Another note that the web service gets deployed to IIS when you build the project.

Here the links:

AdventureWorks Database

http://msftdbprodsamples.codeplex.com/

Sample Application

http://dl.dropbox.com/u/4406115/Devs4Devs%202011/AdventureService.zip

Presentation Slides

http://dl.dropbox.com/u/4406115/Devs4Devs%202011/WCF%20AppFabric%20Cache.pptx

Any feedback or questions are welcomed. Until the next talk!

Disqus