gpt4 book ai didi

c# - ASP.Net Core BackgroundService被无故取消

转载 作者:行者123 更新时间:2023-12-05 06:24:46 27 4
gpt4 key购买 nike

我开发了一个BackgroundService,一开始还可以。但在 20 分钟后,由于某种我不知道的原因,它被取消了。特别是对于这种情况,“工作”应该是无限的并且永远不会完成。拜托,你能给点想法吗?我会感激的

public class CheckItemsService : BackgroundService
{
private IConfiguration Configuration { get; }

public CheckItemsService(IConfiguration configuration)
{
Configuration = configuration;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
stoppingToken.Register(() => Log.Information("Job is stopping"));

while (!stoppingToken.IsCancellationRequested)
{
await UpdateItems();

await Task.Delay(1000 * 60, stoppingToken);
}
}

private async Task UpdateItems()
{
var optionsBuilder = new DbContextOptionsBuilder<WalletContext>();
optionsBuilder.UseSqlServer(Configuration.GetSection("ConnectionStrings:Default").Value);
DbContext _context = new DbContext(optionsBuilder.Options);

var items = await _context.EntityX.ToListAsync();

foreach (var item in items)
{
item.ModificationDate = DateTime.Now;
}

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException dbEx)
{
Log.Information(dbEx.Message);
}
}
}

最佳答案

@Scott Chamberlain 谢谢,根据你的评论,你让我想到了搜索主机关闭的原因,所以我发现 IIS 在每个站点中都有一个默认配置,即“不活动”20 分钟。我改变了这个并且它工作得很好。@Vidmantas Blazevicius 非常感谢,这确实是我遇到问题的原因。感谢您的帮助。

我在 IIS 上找到配置解决方案的链接 → How to prevent IIS from shutting down Web Site when not in use?

关于c# - ASP.Net Core BackgroundService被无故取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540620/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com