'ASP .NET Core background service stops even if idle timeout is 0 and start mode is AlwaysRunning in iis

I have searched too much and read too many posts about this problem.

The scenario of my app is to listen VOIP server event logs and send these logs to clients with SignalR. It works properly until event logs finished at the end of the day but at beginning the next day it won't start again.

I tried both Background Service with ExecuteAsync and IHostedService with StartAsync but it doesn't changed the result.

Here is the AppPool settings for my app on iis.

enter image description here

If I stop/start the app's site on IIS and open it in browser on server it will starts again.

Is it a known issue in Asp.Net background service? Any idea would be appreciated.



Solution 1:[1]

Best way for keep your service live in IIS is create an ping service and call an GET HTTP request every 10 sec your home page.

IIS keep live your app base on request and if your app has not any request it will shotdown it.

create an Pinger background service and call your self!

simple ping service:

 public class Pinger : BackgroundService
    {
        private readonly ILogger<Pinger> _logger;

        public Pinger(ILogger<Pinger> logger)
        {
            _logger = logger;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                using (var httpClient = new HttpClient())
                {
                    await httpClient.GetAsync("https://sample.com/", stoppingToken);
                    _logger.LogInformation($"Ping at {DateTime.UtcNow:s}");
                }

                await Task.Delay(10 * 1000, stoppingToken);
            }
        }
    }

in program.cs

 Host.CreateDefaultBuilder(args)...
.ConfigureServices(services =>
                {
                    services.AddHostedService<Pinger>(); 
                });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1