Click here to Skip to main content
15,887,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am implementing a background service, the problem is when I run it in local, it is working fine since the app is hit and running. But, in IIS, I deployed code and started the server in IIS. The background service is not running until I browse the site and is becoming idle when the server is idle.

I need the background service to run the very moment I restart or start the site.

My program.cs:
C#
builder.Services.AddHostedService<FailedEmailManagmentService>();


I have registered it as well.

What I have tried:

C#
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using handbook.Controllers.HR;
using handbook.Data;
using handbook.Models.Mail;
using handbook.Repositories.Implementation;
using handbook.Repositories.Interface;
using handbook.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Configuration;
using System.Globalization;
using System.Linq;

namespace handbook.BackgroundmailService
{
    public class EmailReminderSenderService : IHostedService, IDisposable
    {
        public static IConfiguration Configuration { get; set; }
        private Timer _timer;
        private readonly IOauthMailService _emailSender;
        private readonly ILogger<EmailReminderSenderService> _logger;

        public EmailReminderSenderService(IOauthMailService emailSender,
        ILogger<EmailReminderSenderService> logger, 
        IServiceProvider serviceProvider, IConfiguration configuration)
        {
            _emailSender = emailSender;
            _logger = logger;
            Services = serviceProvider;
            Configuration = configuration;
        }

        public IServiceProvider Services { get; }

        private static TimeSpan getJobRunDelay()
        {
            // Change the delay to run every 10 minutes
            return TimeSpan.FromMinutes(1);
        }

        public void Dispose()
        {
            _timer?.Dispose();
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Background service is started");
            _timer = new Timer(SendEmails, null, 
                               getJobRunDelay(), getJobRunDelay());
            return Task.CompletedTask;
        }
public async void SendEmails(object state)
        {
            my task
        }
public Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Background service is stopping");
            _timer?.Change(Timeout.Infinite, 0);
            return Task.CompletedTask;
        }
    }
}
Posted
Updated 13-Sep-23 7:04am
v2
Comments
[no name] 13-Sep-23 12:17pm    
If you have SQL Server on the web site, you can use SQL Agent to trigger / schedule any ".exe", bat file, etc.
Richard Deeming 14-Sep-23 6:22am    
Have you set the app pool's "start mode" to "AlwaysRunning"?
Sandeep Akhare 15-Sep-23 12:55pm    
Why are you running the service in IIS? when it cab be run as windows/linux service.(deamon job)

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900