【问题标题】:WCF - decrease the timeout for WCF connectionWCF - 减少 WCF 连接的超时时间
【发布时间】:2018-06-19 17:28:33
【问题描述】:

我有一个 WCF 服务,并且我定义了一个名为 IsAlive() 的操作合同方法。目的是在执行其回调函数之前检查客户端是否还活着。

      if (IsClientAlive())
                        WcfService.Callback.UIMessageOnCallback(UIMessage);

默认超时设置为 1 分钟,在我的情况下太长了。由于所有事情都发生在同一个工作站中,我想将它减少到 5 秒,这样执行时就会有一点延迟。

按照此处的说明进行操作 https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/configuring-timeout-values-on-a-binding

我相应地修改了我的 app.config,但之后没有任何改变。 IsAlive() 函数需要 1 分钟才能完成。 知道我错过了什么吗?

<?xml version="1.0" encoding="utf-8" ?><configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>  <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding openTimeout="00:00:05"
             closeTimeout="00:00:05"
             sendTimeout="00:00:05"
             receiveTimeout="00:01:00">
    </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service name="EEAS.Kiosk.WcfService">
    <endpoint address="" binding="wsDualHttpBinding" contract="EEAS.Kiosk.IWcfService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733/KioskService/WcfService/" />
      </baseAddresses>
    </host>
  </service>
</services>  </system.serviceModel></configuration>

注意:当它必须是“wsDualHttpBinding”时,我拼错了“wsHttpBinding”。感谢斯图尔特的提示!我做了更改,但 IsAlive() 函数总是抛出异常“会话通道超时”。

{System.ServiceModel.CommunicationObjectAbortedException:“IsAlive”操作无法完成,因为会话通道等待接收消息时超时。要增加超时时间,可以在配置文件中设置绑定的 receiveTimeout 属性,或者直接在 Binding 上设置 ReceiveTimeout 属性。

所以我将 ReceiveTimeout 超时设置回 1 分钟,但错误仍然存​​在。知道会发生什么吗?我检查了变量 host.State,它的值是 CommunicationState.Opened。

【问题讨论】:

  • 可能是因为您在wsHttpBinding 上设置了值,但实际上使用的是wsDualHttpBinding
  • 考虑到一切都是本地主机,你应该使用命名管道或 TCP 绑定
  • @stuartd 。感谢你及时的答复。我也刚看到。但现在我有一个不同的问题,连接总是超时。我增加到 25 秒,但在某些时候 IsAlive 函数返回 false。似乎客户端在定义的超时后断开连接。

标签: c# wcf binding timeout


【解决方案1】:

这个解释给了我最后的提示。 https://www.rauch.io/2015/06/25/all-wcf-timeouts-explained/

所以我每 15 秒使用一个计时器在客户端实现一次心跳。 receiveTimeout 设置为 1 分钟,因此它可能会在断开连接之前错过 3 个心跳。

在客户端的main方法中:

       System.Timers.Timer keepAliveTimer = new System.Timers.Timer(15000);
        keepAliveTimer.AutoReset = false;
        keepAliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(keepAliveTimer_Elapsed);
        keepAliveTimer.Start();

以及调用WCF服务中isAlive方法的函数。

        void keepAliveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            client.isAlive();
        }
        catch (System.Exception ex)
        {
            eventLog1.WriteEntry(" [CRITICAL EXCEPTION in keepAliveTimer_Elapsed()]. WCf service is down, Kiosk Tray Agent is trying to send a heartbeat." + Environment.NewLine + "Caught an exception of type" + ex.GetType().Name, EventLogEntryType.Error, 912);
        }
        finally
        {
            ((System.Timers.Timer)sender).Start();
        }
    }

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    相关资源
    最近更新 更多