【发布时间】: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是一个竞争条件。当你执行基于状态分支的逻辑时,它可能不是同一个状态。