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!

Categories: ,

Disqus