Wednesday, June 17, 2009

WCF error: System.ServiceModel.CommunicationException: The socket connection was aborted

I wrote my first WCF service recently which supported tcp as a transport protocol. During testing I faced a very strange problem. The service used to work for sometime and then suddenly would start throwing Communication exception.
System.ServiceModel.CommunicationException: 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
This error was very annoying and there was no clue what is going wrong. While googling for this, I came across some posts which talked about this issue. Every WCF channel has a receive timeout whose value is picked up from the app.config file when you add proxy for the service. If there is no activity during this timeout then WCF closes underlying socket and so further call on the channel throws Communication exception.
To resolve this issue, set the receive timeout to appropriate value at both client as well as server endpoints. You can use 'infinite' keyword to suggest WCF to use TimeSpan.MaxValue value. 'infinite' keyword is not recognized by Visual Studio editor and would show error for that which can be safely ignored. Another way to set infinite timeout is programmatically. At both endpoints (i.e. at client and server) set the endpoint's receivetimeout propery to TimeSpan.MaxValue.
Below is good posts on the same issue -
http://social.msdn.microsoft.com/forums/en-US/wcf/thread/06cb1522-31f0-4ce3-85f0-02656228a8e1/

No comments: