In the HTTP Connection header, my web service client is including:
Connection: Keep-Alive
I want to disable this. After doing some research, it appears the way to do this is to set the KeepAlive member of the SoapHttpChannelOptions
class to false. But, I do not see a way to access/modify SoapHttpChannelOptions
in the webservice client class that was generated for me in Visual Studio using WSE3.0 (Web Service Enhancement.
In my case, the generated stub class extends Microsoft.Web.Services3.WebServicesClientProtocol
I've been unable to find any examples searching google and most members of the SoapHttpChannelOptions class are inherited into the WebServicesClientProtocol class...
SoapHttpChannelOptions Reference
WebServicesClientProtocol Reference
Note: I'm not trying to modify the web server. I'm trying to modify the web service client.
One solution is to override the GetWebRequest(Uri uri)
method.
The information which lead me to this solution was found on this MSDN Forum Post
Method 1: Modify the Auto Generated file.
Paste this snippet into the Reference.cs file which was automatically generated for you. The downside of this approach is if you ever regenerate the web service client adapters (ie Update Web References) you will have to then modify 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. Modify the namespace and class name so they match your webservice.
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 do not use WSE. I was able to paste the method above under the non WSE webservice class... which extends System.Web.Services.Protocols.SoapHttpClientProtocol
. From my testing it appeared that this made it not include any Http Connection line at all where as when I did it inside a WSE class (which are derived from Microsoft.Web.Services3.WebServicesClientProtocol
) it then included a "Connection: Close" line. According to this site on HTTP KeepAlive:
Under HTTP 1.1, the official keepalive method is different. All connections are kept alive, unless stated otherwise with the following header: Connection: close
So, while it may not include KeepAlive in the header anymore... I think in HTTP1.1 it's assumed to be the default.
来源:https://stackoverflow.com/questions/1527407/how-do-i-prevent-my-net-soap-client-from-including-connection-keepalive-in-t