.NET Core Hosted services timeout

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



.NET Core Hosted services timeout



I've started using .NET Core Hosted services to handle some pub/sub functionality. I plan to use cancellation tokens to shut the services down. And I wonder whether there is some timeout for them? I want the service to run forever if it has not been explicitly stopped.



Appreciate your help.


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);





Please do it as I described in my linked answer. You are running this now in another background-task. Just have a while-loop after your code that checks for cancellation-token being cancelled and put a delay in there.
– alsami
Aug 6 at 16:35





@alsami, thank you. Do you know why it does not stop in the case with Task.Factory.StartNew ?
– Denys Alexieiev
Aug 6 at 16:39





Each task you create and run, runs in the thread-pool. Is therefore not stopped with the hosted-service. owlcation.com/stem/C-ThreadPool-and-its-Task-Queue-Example
– alsami
Aug 6 at 16:44





1 Answer
1



If you check out my accepted answer here, and your hostedservice kinda looks like that, you can just do the following within the while-loop contained in the ExecuteAsync method.


ExecuteAsync


await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);



The method must be marked as async of course.





And if I register it as services.AddHostedService<ClientHostedService>(); then what?
– Denys Alexieiev
Aug 6 at 15:19






Well I don't know why they did that, but that extension method is not registering the hosted-service as a singleton. It registers the hosted service as transient. public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services) where THostedService : class, IHostedService return services.AddTransient<IHostedService, THostedService>();
– alsami
Aug 6 at 15:28






So from your main answer I see that you suggest making Task.Delay, however, when I do not do this in my ExecuteAsync the service is not being shut down - it continues handling messages. My assumption is that it does not stop when ExecuteAsync finished working. But I am not sure whether it stops after some timeout. I run it for 10 minutes approximately - it did not stop.
– Denys Alexieiev
Aug 6 at 15:43





Well it does stop, you have to pass the cancellationtoken and you have to await it. It will stop when you shut down the server, but only then.
– alsami
Aug 6 at 15:45






@alsami the service acts as a singleton. It's managed, started, stopped by the HostedServiceExecutor class, which is a singleton
– Panagiotis Kanavos
Aug 6 at 15:49






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered