后台服务未启动.net core并在iis windows服务器上发布

问题描述 投票:0回答:2

我有一个后台服务,它通过使用 NCrontab 库在特定时间作为作业工作 我的项目使用.net 7 这是我的后台服务

public class GenerateSiteMapXMLFileBackgroundService : BackgroundService, IHostedService
    {
        private CrontabSchedule _schedule;
        private DateTime _nextRun;
        public IServiceScopeFactory _serviceScopeFactory;

        private string Schedule => "00 20 00 * * *"; //Runs every day at 00:20:00 (cron expression) {ss mm hh dd mm yy}
        public GenerateSiteMapXMLFileBackgroundService(IServiceScopeFactory serviceScopeFactory, IServiceProvider services)
        {
            _schedule = CrontabSchedule.Parse(Schedule, new CrontabSchedule.ParseOptions { IncludingSeconds = true });
            _nextRun = _schedule.GetNextOccurrence(DateTime.Now);
            _serviceScopeFactory = serviceScopeFactory;
            Services = services;
        }
        public IServiceProvider Services { get; }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var scope = Services.CreateScope())
            {
                var scoped = scope.ServiceProvider.GetRequiredService<ISiteMapeService>();
                do
                {
                    var now = DateTime.Now;
                    if (now > _nextRun)
                    {
                        await scoped.CreateSiteMapFile();
                        _nextRun = _schedule.GetNextOccurrence(DateTime.Now);
                    }
                    await Task.Delay(5000, stoppingToken); //5 seconds delay
                }
                while (!stoppingToken.IsCancellationRequested);
            }
        }
    }

我已经在startup.js中注册了它

services.AddHostedService<GenerateSiteMapXMLFileBackgroundService>();

当我在本地测试它并设置我想要的时间时,它运行良好并启动 但是当我在 iis 服务器上发布我的项目时 它不起作用 我应该在iis服务器上做任何配置吗?

我想我必须在iis服务器上做一些配置 但我不知道

c# iis background-service .net-7.0 windows-server-2022
2个回答
2
投票

默认情况下,IIS 将在一段时间不活动后关闭应用程序池。如果不配置 IIS,您的后台工作人员将无法按预期工作。

## 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
}

或者如果您喜欢 GUI,只需在 IIS 中编辑设置。

信用


0
投票

这很简单。只需转到 .NET 项目中的“Launchsettings.json”文件即可。在“IIS Express”配置部分中,添加行:“ApplicationUrl:(部署 .NET 项目的域)”。然后,在 IIS 上重新部署应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.