我已经开始使用 .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);
}