How can I disable keep-alive on ASP.NET Web Service client requests?

11,086

Isn't overriding the GetWebRequest method working:

protected override WebRequest GetWebRequest(Uri uri)
{
    var webRequest = (HttpWebRequest)base.GetWebRequest(uri);
    webRequest.KeepAlive = false;
    return webRequest;
}
Share:
11,086
Matt Brindley
Author by

Matt Brindley

CTO at http://litmus.com/ - Litmus makes it easy to build, test, and monitor every email for the best results. I'm a C# developer who loves to dabble in Ruby and Objective-C.

Updated on June 17, 2022

Comments

  • Matt Brindley
    Matt Brindley almost 2 years

    I have a few web servers behind an Amazon EC2 load balancer. I'm using TCP balancing on port 80 (rather than HTTP balancing).

    I have a client polling a Web Service (running on all web servers) for new items every few seconds. However, the client seems to stay connected to one server and polls that same server each time.

    I've tried using ServicePointManager to disable KeepAlive, but that didn't change anything. The outgoing connection still had its "connection: keep-alive" HTTP header, and the server kept the TCP connection open. I've also tried adding an override of GetWebRequest to the proxy class created by VS, which inherits from SoapHttpClientProtocol, but I still see the keep-alive header.

    If I kill the client's process and restart, it'll connect to a new server via the load balancer, but it'll continue polling that new server forever.

    Is there a way to force it to connect to a random server each time? I want the load from the one client to be spread across all of the web servers.

    The client is written in C# (as is the server) and uses a Web Reference (not a Service Reference), which points to the load balancer.