【问题标题】:WCF client apps started to constantly receive timeout exceptionsWCF 客户端应用程序开始不断收到超时异常
【发布时间】:2018-08-14 09:14:24
【问题描述】:

我遇到了 WCF 服务器端的问题。 它开始于大约一个月前,我们之前没有发现(大约 1 年)。现在我们的客户端应用程序开始不断收到超时异常。 客户端和服务是桌面 WPF 应用程序。服务托管在远程服务器上。 通过调试(附加到主机进程)和日志(.svclog),我们发现有时我们的主机会在短时间内收到来自同一 IP 地址的大量请求(并且处理它们需要多个线程)。 10 个请求中有 9 个因通信异常而失败。在每种情况下,开始和失败之间的时间都是不同的。它从 20 分钟到 1 小时不等。 同时其他客户端无法连接到服务。我怀疑会发生这种情况,因为所有可用线程都与一个客户端一起工作,直到它失败。 我们手动修改了 ConcurrencyMode、UseSynchronizationContext,我们没有修改主机配置中的发送、打开、接收、关闭超时,因为据我所知,它们应该默认为 1 分钟。 我们想中断长请求或只允许一个线程处理它们。

我们的服务配置: .cs 服务文件

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall, AddressFilterMode = AddressFilterMode.Any, UseSynchronizationContext = false)] 

app.config

<system.serviceModel>
     <bindings>
       <wsHttpBinding>
         <binding name="htWsHttpEndpointBinding" maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="XService" behaviorConfiguration="htServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://ip/Xservice" />
          </baseAddresses>
        </host>   
        <endpoint address="http://ip/Xservice" binding="wsHttpBinding" bindingConfiguration="htWsHttpEndpointBinding" contract="IXService" />
        <endpoint address="mex" binding="wsHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="htServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors> 
   </system.serviceModel>

客户端app.config

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IXService" maxReceivedMessageSize="2147483647">
          <security mode="None"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://ip/XService"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IXService"
                contract="IXService" name="WSHttpBinding_IXService"/>         
    </client>
  </system.serviceModel>

【问题讨论】:

  • 您是否适当地 Close() / Abort() 代理客户端实例?
  • @Crowcoder 我们使用来自codeproject的类
  • 不太确定这是最佳做法。请参阅该文章的第一条评论。此外,CommunicationState 是一个竞争条件。当你执行基于状态分支的逻辑时,它可能不是同一个状态。

标签: c# wcf


【解决方案1】:
ConcurrencyMode = ConcurrencyMode.Single //set to multiple

您可能遇到网络问题,导致重试和彼此死锁。一般来说,如果托管在 WPF 应用程序中,您的 WCF 代码应该异步运行,否则您可能会因连接失败而陷入死锁。

【讨论】:

  • 我之前尝试过修改该参数,但并没有解决问题。我将它恢复为 Single,因为它可以正常工作很长时间。不过我知道它会造成什么问题,所以我将其设置为 Multiple。
  • 听起来仍然像是托管服务的 STA 应用程序的常见死锁。在单独的线程中启动服务并避免从服务实现到 UI 的同步调用(例如,属性更改等)-> 可能很难深入。
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多