我已经开始使用.NET Core托管服务来处理某些发布/订阅功能。我计划使用取消令牌来关闭服务。我想知道他们是否有超时时间?如果服务尚未明确停止,我希望它永远运行。
感谢您的帮助。
protected override Task ExecuteAsync(CancellationToken cancellationToken)
{
return Task.Factory.StartNew(
() =>
{
var redisPubSub = new RedisPubSubServer(ClientsManager, Constants.RedisChannel)
{
OnMessage = (channel, msg) =>
{
Trace.WriteLine($"Received '{msg}' from channel '{channel}'");
var message = JsonConvert.DeserializeObject<Message>(msg);
_hubContext.Clients.User(message.UserId.ToString()).SendAsync("ReceiveMessage", message, cancellationToken);
},
OnUnSubscribe = (message) =>
{
Trace.WriteLine($"OnUnSubscribe returns {message}");
},
OnError = (exception) =>
{
Trace.WriteLine($"OnError returns {exception}");
},
OnStart = () =>
{
Trace.WriteLine($"OnStart has been fired.");
},
OnStop = () =>
{
Trace.WriteLine($"OnStop has been fired");
}
};
redisPubSub.Start();
Trace.WriteLine($"OnStop has been fired {redisPubSub.WaitBeforeNextRestart} {redisPubSub.HeartbeatTimeout} {redisPubSub.HeartbeatInterval}");
}, cancellationToken);
}
答案 0 :(得分:0)
如果您查看我接受的答案here,并且您的hostedservice有点类似,则可以在ExecuteAsync
方法中包含的while循环内执行以下操作。
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
该方法当然必须标记为异步。