Thursday, January 17, 2008

WebExceptions and you

Every now and then, one of my applications was throwing up an error of type 'System.Net.WebException: The request was aborted: The request was canceled'.

This happened during execution of a SOAP webservice call towards a backend system. Problem was, it was almost impossible to reproduce, happening as it did on irregular, non-deterministic moments. I only noticed it because of an automatic error reporter, which automatically mails any exceptions to a public Outlook folder.

After finding some time to investigate the issue, I found that it was apparently a known issue, related to the Keep-Alive property of the HTTP Request itself. Apparently, every now and then, IIS ate the webservice request, when Keep-Alive was true.

One possible solution was of course to disable Keep-Alive on the IIS level. While this would no doubt solve the problem, it is kinda overkill and can have negative implications on the performance of the application as a whole (Keep-Alive is there for a reason, you know).

A better solution is to disable Keep-Alive for the SOAP request only, and leave it on for the other requests. This was slightly complicated by the fact that - like most .NET users - we don't actually code our webservice consumption classes ourself, but use generated Proxy classes, obtained by either wsdl.exe or by adding a Web Reference in VS.NET. Luckily, by means of a simple subclass, we could dance around this problem and keep our code generation intact.


public class MyCustomService : MyGeneratedService
{
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
request.KeepAlive = false;

return request;
}
}


After introducing these changes for each SOAP request, the intermittent WebException has dissapeared. Life is good.

No comments: