Turning KeepAlive Off

We've run into strange WebExceptions being thrown in our application when we issue mutiple web service requests asynchronously. It's not common, but it's not uncommon either. One team member found this idea, which basically says, "turn KeepAlive off". Of course, how do you do this in a class that was generated by wsdl.exe?

Simple. Just open up the derived SoapHttpClientProtocol class, and add this code:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
  System.Net.WebRequest request = base.GetWebRequest(uri);
 
  if(typeof(System.Net.HttpWebRequest).IsAssignableFrom(request.GetType()))
  {
   (request as System.Net.HttpWebRequest).KeepAlive = false;
 }
 
  return request;
}

request is usually an HttpWebRequest but I didn't want to take any chances :).

We don't know if this is going to work yet, but at least it was easy to modify.

* Posted at 04.26.2007 12:31:08 PM CST | Link *

Blog History