.net 如何在 WCF 中为 HTTP 连接设置保持活动间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3466490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:36:32  来源:igfitidea点击:

How to set keep alive interval for HTTP connection in WCF

.netwcf

提问by Ladislav Mrnka

Http transport channel in WCF uses persistent HTTP connections by default. How to control keep alive timeout for those connections? Default value is 100s. I found that value by monitoring application in Procmon. I haven't found any setting in http transport binding element which configures this timeout. Is there any .NET class which can control that timeout?

WCF 中的 Http 传输通道默认使用持久性 HTTP 连接。如何控制这些连接的保持活动超时?默认值为 100 秒。我通过监视 Procmon 中的应用程序发现了这个价值。我没有在 http 传输绑定元素中找到任何配置此超时的设置。是否有任何 .NET 类可以控制超时?

回答by NakedBrunch

回答by Jacob

It sounds to me like you're wanting to adjust the Keep-AliveHTTP header. You should read this articleon HTTP keep-alive and first determine if this is really something worth your time.

在我看来,您想调整Keep-AliveHTTP 标头。您应该阅读有关 HTTP keep-alive 的这篇文章,并首先确定这是否真的值得您花时间。

If it is, try creating a Message Inspector. This should allow you to modify the HTTP headers for each message that gets sent out:

如果是,请尝试创建Message Inspector。这应该允许您修改发送的每条消息的 HTTP 标头:

public class KeepAliveMessageInspector : IClientMessageInspector
{
    // ...

    public object BeforeSendRequest(
        ref System.ServiceModel.Channels.Message request,
        System.ServiceModel.IClientChannel channel)
    {
        // Add/modify the Keep-Alive header
        var httpRequestMessage = new HttpRequestMessageProperty();
        httpRequestMessage.Headers.Add("Keep-Alive", "9000");
        request.Properties.Add(
            HttpRequestMessageProperty.Name, httpRequestMessage);
        return null;
    }

    // ...
}

回答by Luiz Felipe

I found the solution for this. The problem is that the underlying kernel http.sys has it's own timeout and it will break the connection.

我找到了解决方案。问题是底层内核 http.sys 有它自己的超时,它会断开连接。

http://mmmreddy.wordpress.com/2013/07/11/wcf-use-of-http-transport-sharing-persistent-tcp-sessions/

netsh http add timeout timeouttype=idleconnectiontimeout value=120

Also, this question is similar to this What 130 second timeout is killig my WCF streaming service call?

此外,这个问题类似于这个什么 130 秒超时会杀死我的 WCF 流服务调用?

回答by WenningQiu

Setting ServicePoint.MaxIdleTime should let you change the default 100 second idle timeout on the persistent HTTP connections.

设置 ServicePoint.MaxIdleTime 应该可以让您更改持久 HTTP 连接上的默认 100 秒空闲超时。

https://www.visualbasicplanet.info/windows-communication/configuring-http-connections.html

https://www.visualbasicplanet.info/windows-communication/configuring-http-connections.html

回答by louisgab

From this answer, and what I can read here, it looks like you want to create a custom http binding, and set the inactivityTimeoutattribute.

这个答案以及我可以在这里阅读到的内容,您似乎想要创建一个自定义 http 绑定并设置该inactivityTimeout属性。

From the MSDN article:

来自 MSDN 文章:

<bindings>
  <customBinding>
    <binding name="Binding1">
      <reliableSession acknowledgementInterval="00:00:00.2000000" enableFlowControl="true"
                        maxTransferWindowSize="32" inactivityTimeout="00:10:00" maxPendingChannels="128"
                        maxRetryCount="8" ordered="true" />
      <security mode="None"/>
      <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                    hostNameComparisonMode="StrongWildcard" 
                    proxyAuthenticationScheme="Anonymous" realm="" 
                    useDefaultWebProxy="true" />
    </binding>
  </customBinding>
</bindings>
<bindings>
  <customBinding>
    <binding name="Binding1">
      <reliableSession acknowledgementInterval="00:00:00.2000000" enableFlowControl="true"
                        maxTransferWindowSize="32" inactivityTimeout="00:10:00" maxPendingChannels="128"
                        maxRetryCount="8" ordered="true" />
      <security mode="None"/>
      <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                    hostNameComparisonMode="StrongWildcard" 
                    proxyAuthenticationScheme="Anonymous" realm="" 
                    useDefaultWebProxy="true" />
    </binding>
  </customBinding>
</bindings>

回答by Shawn de Wet

How about this? Seems it may be a function of your IIS Server and nothing to do with the WCF service? I know this link applies to IIS6, but maybe it serves as a foundation to something similar in IIS7? http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/73566f83-c257-4941-8ed8-7ae45b2e7985.mspx?mfr=true

这个怎么样?似乎它可能是您的 IIS 服务器的功能而与 WCF 服务无关?我知道此链接适用于 IIS6,但也许它可以作为 IIS7 中类似内容的基础? http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/73566f83-c257-4941-8ed8-7ae45b2e7985.mspx?mfr=true