C# WCF回调计时器
我有一个托管在Windows服务中的WCF服务。这使用发布-订阅模式将事件发布到多个订阅者。然而,我发现在一段时间的不活动之后,连接失败,我得到的可靠会话出现错误。我试图通过更改非活动超时和接收超时设置来解决这个问题,但没有任何区别。老实说,我快疯了 我看到的一个解决方案是每隔一段时间“ping”所有订户。(例如,超时长度的一半) 设置这样的“ping”的最佳方式是什么?i、 e.如何向服务添加计时器,以及如何让计时器调用ping函数 编辑: 我对ping解决方案不太满意,我想进一步调查一下为什么我的可靠会话一直超时。下面是此服务的绑定配置 服务器app.configC# WCF回调计时器,c#,.net,wcf,C#,.net,Wcf,我有一个托管在Windows服务中的WCF服务。这使用发布-订阅模式将事件发布到多个订阅者。然而,我发现在一段时间的不活动之后,连接失败,我得到的可靠会话出现错误。我试图通过更改非活动超时和接收超时设置来解决这个问题,但没有任何区别。老实说,我快疯了 我看到的一个解决方案是每隔一段时间“ping”所有订户。(例如,超时长度的一半) 设置这样的“ping”的最佳方式是什么?i、 e.如何向服务添加计时器,以及如何让计时器调用ping函数 编辑: 我对ping解决方案不太满意,我想进一步调查一下为什
<binding name="WSDualHttpBinding_IError" receiveTimeout="24.20:31:23.6470000">
<reliableSession inactivityTimeout="24.20:31:23.6470000" />
</binding>
<binding name="WSDualHttpBinding_IError" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="24.20:31:23.6470000"
sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="24.20:31:23.6470000" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
要实现客户端的ping,您可能需要遵循以下步骤列表:
List<OperationContext> _clientCtxList = new List<OperationContext>();
void IMyService.InitSession() {
_clientCtxList.Add(OperationContext.Current);
}
- 这是记事本代码-它可能包含语法错误,并且可能无法编译
- 不包括异常处理
- 不包括锁定_clineCtxList,但强烈建议锁定
[OperationContract]
void Ping();
List<OperationContext> _clientCtxList = new List<OperationContext>();
void IMyService.InitSession() {
_clientCtxList.Add(OperationContext.Current);
}
void tmrPing (object state) {
foreach (var ctx in _clientCtxList)
{
// todo: catch exceptions and remove client context
// from list in case of failure
ctx.GetCallbackChannel<IMyCallbackContract>().Ping();
}
// restart timer
_timer.Change(10000, Timeout.Infinite);
}
System.Threading.Timer _timer;
void Startup() {
// call my function in 10 seconds
_timer = new System.Threading.Timer(
tmrPing, null, 10000, Timeout.Infinte);
}