dantey89
dantey89

Reputation: 2287

Prevent Asp Net Core 3 application from being stopped/unloaded/disposed

I have my web-application developed with .net core 3. I deploying it on hosting which is running IIS. One of the most important modules of my application is scheduled tasks that should executes during all application lifetime. The issue I faced is that application stopping after 1 or 2 hours of inactivity.

I deploy the application with the 'out-of-process' approach. To reach the goal I initializing application with this code at the Program.cs:

Host.CreateDefaultBuilder(args)               
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseIISIntegration();
                    webBuilder.UseEnvironment(EnvironmentName);
                    webBuilder.UseStartup<Startup>();
                });   

Also, I put these settings to the .scproj

 <PropertyGroup>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
  </PropertyGroup>

Then I check my headers I can see this entry in my response headers:

Server: Kestrel

My schedules are implemented IHostedService and should run as background tasks.

All the above gives me the opinion that the application should run non-stop and shouldn't be disposed by IIS because it's running at the Kestrel and use IIS as a proxy server only. But in reality, it is different. Using logs I can see how after an hour or two my application stops running and does not log anymore.

Also, I don't have an access to the IIS because it is out of my permission, so I can't change it's setting. Does anybody have any idea how to get my application to run non-stop?

Upvotes: 2

Views: 3383

Answers (2)

Eric
Eric

Reputation: 2380

You will need to set these IIS settings to prevent your application from automatically being recycled due to inactivity.

App Pool Settings:

  1. change Start Mode to AlwaysRunning – this makes the service start up immediately, instead of waiting for a request
  2. change Idle Time-out to 0 - This will allow the service to continue to run, even though no requests are coming into it. Otherwise, the service will shut down after the configured number of minutes due it receiving no new requests.
  3. change Regular Time Interval to 0 – this will prevent the service from regularly restarting on the interval

Website settings: On the Advanced Settings for the website, change Preload Enabled to true.

Upvotes: 5

Farhad Zamani
Farhad Zamani

Reputation: 5861

Better way is change the iis settings.

Run this powershell script to keep alive application in iis. and if your server has been restarted, by this script your application run automatically

  • $AppPool is name of your application in Application Pools
  • $Site is name of your application in Sites
## IIS WebAdmin Module
Import-Module WebAdministration

$AppPoolInstance = Get-Item IIS:\AppPools\$AppPool

Write-Output "Set Site PreLoadEnabled to true"
Set-ItemProperty IIS:\Sites\$Site -name applicationDefaults.preloadEnabled -value True

Write-Output "Set Recycling.periodicRestart.time  = 0"
$AppPoolInstance.Recycling.periodicRestart.time = [TimeSpan]::Parse("0");
$AppPoolInstance | Set-Item

Write-Output "Set App Pool start up mode to AlwaysRunning"
$AppPoolInstance.startMode = "alwaysrunning"

Write-Output "Disable App Pool Idle Timeout"
$AppPoolInstance.processModel.idleTimeout = [TimeSpan]::FromMinutes(0)
$AppPoolInstance | Set-Item

if ($appPoolStatus -ne "Started") {
    Write-Output "Starting App Pool"
    Start-WebAppPool $AppPool
} else {
    Write-Output "Restarting App Pool"
    Restart-WebAppPool $AppPool
}

This link maybe helpful for you

Upvotes: 0

Related Questions