如何防止我的 .NET SOAP 客户端包含“Connection: KeepAlive”在 HTTP 标头中。 (使用WSE3.0)

发布于 2024-08-06 08:24:13 字数 835 浏览 6 评论 0原文

在 HTTP 连接标头中,我的 Web 服务客户端包括: 连接:保持活动

我想禁用此功能。 经过一些研究,似乎实现此目的的方法是将 SoapHttpChannelOptions 类的 KeepAlive 成员设置为 false。但是,我没有看到在使用 WSE3.0(Web 服务增强)在 Visual Studio 中为我生成的 Web 服务客户端类 中访问/修改 SoapHttpChannelOptions 的方法。

就我而言,生成的存根类扩展了 Microsoft.Web.Services3.WebServicesClientProtocol

我在谷歌搜索中找不到任何示例,并且 SoapHttpChannelOptions 类的大多数成员都继承到 WebServicesClientProtocol 类中...

SoapHttpChannelOptions 参考
WebServicesClientProtocol 参考

注意:我没有尝试修改网络服务器。我正在尝试修改网络服务客户端。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-08-13 08:24:13

一种解决方案是重写 GetWebRequest(Uri uri) 方法。
引导我找到此解决方案的信息在此 MSDN 论坛帖子

方法一:修改自动生成的文件。

将此代码段粘贴到自动为您生成的 Reference.cs 文件中。这种方法的缺点是,如果您重新生成 Web 服务客户端适配器(即更新 Web 引用),则必须再次修改该文件。

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

方法 2:创建分部类

创建一个文件并将以下代码粘贴到其中。修改命名空间和类名称,使其与您的 Web 服务匹配。

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;
        }
    }
}

备注

如果您不使用 WSE,此方法可能有效。我能够将上面的方法粘贴到非 WSE Webservice 类下...它扩展了 System.Web.Services.Protocols.SoapHttpClientProtocol。从我的测试来看,这使得它根本不包含任何 Http Connection 行,就像我在 WSE 类(派生自 Microsoft.Web.Services3.WebServicesClientProtocol)中执行此操作时,它然后包含“连接:关闭”行。根据 HTTP KeepAlive 网站

HTTP 1.1下,官方的keepalive
方法不同。所有连接
除非另有说明,否则都保持活力
否则使用以下标头:
连接:关闭

因此,虽然它可能不再在标头中包含 KeepAlive...我认为在 HTTP1.1 中它被假定为默认值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文