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 becoming idle when the server is idle
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 _logger;
public EmailReminderSenderService(IOauthMailService emailSender, ILogger 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;
}
}
}
My program. cs
builder.Services.AddHostedService();
i have registered it also
Prasad RaveendranPosted Sep 16, 2023, 12:31 PM
If you want to ensure that your web application in IIS remains active and does not get unloaded when idle, you can utilize the "Always On" feature in IIS, which is specifically designed for this purpose. Here's how you can enable it:
Open IIS Manager:
Open the Internet Information Services (IIS) Manager on your server. You can do this by searching for "IIS Manager" in the Windows Start menu.
Select Your Web Application:
In the IIS Manager, navigate to your web application under the "Sites" or "Default Web Site" node in the Connections pane on the left-hand side.
Enable "Always On":
In the right-hand pane, under the "Edit Site" section, click on "Advanced Settings."
Locate the "Always On" Setting:
In the "Advanced Settings" dialog that appears, scroll down to find the "Idle Time-out (minutes)" property. By default, it's set to 20 minutes. Click on it to edit.
Set "Always On" to True:
Change the value of "Idle Time-out (minutes)" to "0" (zero). This setting means that your application will not be automatically unloaded due to inactivity.
Apply the Changes:
Click "OK" to apply the changes.
Restart the Application Pool (if needed):
Depending on your IIS configuration, you may need to restart the application pool associated with your web application for the changes to take effect. You can do this by right-clicking on the application pool in the "Application Pools" node in IIS Manager and selecting "Recycle."
The "Always On" setting ensures that your web application remains active even when there is no incoming traffic, preventing it from being unloaded due to inactivity. This is useful for scenarios where you have background processes or tasks that need to run continuously.
Keep in mind that enabling "Always On" consumes server resources, so it's important to monitor the server's performance and resource usage to ensure it can handle the continuous operation of your web application.
Please try this and post your feedback here.
Cr BhargaviPosted Sep 20, 2023, 10:27 AM
you can achieve this using a workaround like running your background service as a Windows Service or a separate console application.
Sam HobbsPosted Sep 16, 2023, 3:51 PM
Sometimes I see someone asking how to get something to work. Something that is intended to solve a problem. Usually it would be better to ask about the fundamental problem. What is the fundamental problem?
As best as I can understand from the code you posted, you need to periodically send email. You probably do not need IIS for that. If the input is from a website then perhaps, within IIS, the data can be put someplace that the service can access without IIS.
Mohammad HussainPosted Sep 14, 2023, 6:54 AM
Running a background service continuously in an IIS-hosted ASP.NET application can be a bit challenging because IIS is primarily designed to handle HTTP requests and may not be ideal for long-running background tasks. However, you can achieve this using a workaround like running your background service as a Windows Service or a separate console application.
Amit MohantyPosted Sep 14, 2023, 5:55 AM
In IIS, running background tasks continuously even when the application is idle can be a bit challenging because IIS is primarily designed for serving web applications and may not be the best tool for running long-running background tasks.
You can create a Windows Service or a separate console application to run your background service. This way, the service will run independently.