作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了一个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/
我有一个小型 Java 11 示例,其中包含 JUnit 5 测试,结果如下: changed conditional boundary → SURVIVED 主类: public final cla
我是一名优秀的程序员,十分优秀!