I've implemented IHostedService in an asp.net core website. It works great but the problem is that I want it to be started when the hosting server boots up or the IIS service restarts but it won't unless the first request to the website comes in.
- The website is hosted on IIS version 10.0.18
- The AppPool is in "AlwaysRunning" mode
- "PreloadEnabled" is "True" on the website.
- Setting ".NET CLR Version" to "No Managed Code" or "v4.0.xxxxxx" did not helped.
- dotnet core version is 2.2 and dotnet core hosting bundle is installed.
UPDATE 1: "Application Initialization Module", Suggested by @Arthur did not help. Neither on site nor server level.
The configuration I used:
<applicationInitialization
doAppInitAfterRestart="true"
skipManagedModules="false"
remapManagedRequestsTo="init.htm">
<add initializationPage="/init.htm" hostName="localhost"/>
</applicationInitialization>
UPDATE 2: Here is how I implemented the interface
internal class PaymentQueueService : IHostedService, IDisposable
{
private readonly ILogger _logService;
private Timer _timerEnqueue;
public PaymentQueueService(ILogger logService)
{
_logService = logService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logService.LogInformation("Starting processing payments.");
_timerEnqueue = new Timer(EnqueuePayments, null, TimeSpan.Zero,
TimeSpan.FromSeconds(10));
return Task.CompletedTask;
}
private void EnqueuePayments(object state)
{
_logService.LogInformation("Enqueueing Payments.");
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logService.LogInformation("Stopping processing payments.");
_timerEnqueue?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timerEnqueue?.Dispose();
}
}
The Program class in main.cs file:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).ConfigureServices(services =>
{
services.AddHostedService<PaymentQueueService>();
}).Configure((IApplicationBuilder app) =>
{
app.UseMvc();
})
.UseStartup<Startup>();
}
The Startup class:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
}
}
Startmode=AlwaysRunning
and the Site toPreload Enabled=true
(both to find in the Advanced Settings). Then you can even Host your siteInProcess
(so no need for an obsoleteOutOfProcess
). Also see stackoverflow.com/a/46573873/1443733