How to prevent my .NET SOAP client from including "Connection: KeepAlive" in the HTTP headers. (using WSE3.0)

In the HTTP Connection header, my web service client includes: Connection: Keep-Alive

I want to disable this. After doing some research, this seems to be done to set the KeepAlive member of the SoapHttpChannelOptions class to false. But I don’t see a way to access / change SoapHttpChannelOptions in the webservice client class that was created for me in Visual Studio using WSE3.0 (Web Service Enhancement.

In my case, the generated stub class extends Microsoft.Web.Services3.WebServicesClientProtocol

I was unable to find google search examples, and most members of the SoapHttpChannelOptions class are inherited in the WebServicesClientProtocol class ...

Link SoapHttpChannelOptions
WebServicesClientProtocol Reference

Note. I am not trying to change the web server. I am trying to change a web service client.

+6
wse keep-alive webservice-client
source share
1 answer

One solution is to override the GetWebRequest(Uri uri) method.
The information that led me to this decision was found on this MSDN forum post.

Method 1: modify the Auto Generated file.

Paste this snippet into the Reference.cs file that was automatically generated for you. The disadvantage of this approach is that if you ever update client web service adapters (like Update Web References), you will then have to change the file again.

 protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri); webRequest.KeepAlive = false; return webRequest; } 

Method 2. Create a partial class

Create a file and paste the following code into it. Change the namespace and class name to match your web service.

 namespace YourNamespace { using System.Diagnostics; using System.Web.Services; using System.ComponentModel; using System.Web.Services.Protocols; using System; using System.Xml.Serialization; /// <summary> /// This partial class makes it so all requests specify /// "Connection: Close" instead of "Connection: KeepAlive" in the HTTP headers. /// </summary> public partial class YourServiceNameWse : Microsoft.Web.Services3.WebServicesClientProtocol { protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri); webRequest.KeepAlive = false; return webRequest; } } } 

Notes

This approach may work if you are not using WSE. I managed to insert this method above in a webservice class other than WSE ... which extends System.Web.Services.Protocols.SoapHttpClientProtocol . From my testing, it turned out that this did not include any Http connection string at all, where when I did it inside the WSE class (which was obtained from Microsoft.Web.Services3.WebServicesClientProtocol ), it included the "Connection: Close" line. According to this site on KeepAlive HTTP :

According to HTTP 1.1, the official system keepalive method is different. All connections are kept alive, unless otherwise indicated with the following heading: Connection: close

So, although it can no longer include KeepAlive in the header ... I think in HTTP1.1 it is considered the default.

+11
source

All Articles