我的第一个问题是Microsoft的这篇文章是否适用于其他程序员:https://blogs.msdn.microsoft.com/cesardelatorre/2017/11/18/implementing-background-tasks-in-microservices-with-ihostedservice-and-the-backgroundservice-class-net-core-2-x/
这里说明了我如何使用后台服务。我有一个具有WEB API的程序,其中将数据输入插入到数据库中。如果数据库中有新数据,我的后台作业应该每分钟显示一次,并且应该处理对其他Web服务的某些请求。
我的问题是,有时它会触发两次或作业未运行。每当后台服务执行时间戳操作时,我都会打印到控制台。一段时间后,不再打印任何内容。
这是我的示例代码。我的代码或配置有问题吗?
第一个代码示例是ConfigureService方法中启动类的代码:
//Adding Background Task Class to Hosted Services in Startup-Class and Method ConfigureServices
services.AddHostedService<JsonDataCreator>();
这是该类的实现,其中包含我的后台服务的逻辑:
/// <summary>
/// Background Service which creates the JSON Files for the machine
/// </summary>
public class JsonDataCreator : IHostedService, IDisposable
{
private readonly ILogger _logger;
private Timer _timer;
/// <summary>
/// Constructor
/// </summary>
/// <param name="logger">Logger functionallity</param>
public JsonDataCreator(ILogger<JsonDataCreator> logger)
{
_logger = logger;
}
/// <summary>
/// Task which is executed Asynchronous
/// </summary>
/// <param name="cancellationToken">Cancellation token for stopping the Task</param>
/// <returns>Task Completed</returns>
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is starting");
Console.WriteLine("Timed Background Service is starting");
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}
/// <summary>
/// Logical Request to Webservice + Database and creation of JSON Files
/// </summary>
/// <param name="state"></param>
private void DoWork(object state)
{
try
{
Console.WriteLine("Begin new Round of Background-Work" + DateTime.Now.ToShortTimeString());
//THERE IS SOME LOGIC INSIDE WHICH CALLS SOME WEBSERVICE
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Stops the Task
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>Task Completed</returns>
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is stopping");
Console.WriteLine("Timed Background Service is stopping");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
/// <summary>
/// Disposes the Task
/// </summary>
public void Dispose()
{
_timer?.Dispose();
}
}
答案 0 :(得分:0)
我通过使用提供的解决方案和无限循环来解决了这个问题。从现在开始,没有关于超时和已取消任务的神秘行为。
解决方案如下:
/// <summary>
/// Background Service which creates the JSON Files for the machine
/// </summary>
public class JsonDataCreator : IHostedService, IDisposable
{
private readonly ILogger _logger;
private Timer _timer;
/// <summary>
/// Constructor
/// </summary>
/// <param name="logger">Logger functionallity</param>
public JsonDataCreator(ILogger<JsonDataCreator> logger)
{
_logger = logger;
}
/// <summary>
/// Task which is executed Asynchronous
/// </summary>
/// <param name="cancellationToken">Cancellation token for stopping the Task</param>
/// <returns>Task Completed</returns>
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is starting");
while(true){
//Do the business logic
//Wait the time you want
}
return Task.CompletedTask;
}
/// <summary>
/// Stops the Task
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>Task Completed</returns>
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is stopping");
Console.WriteLine("Timed Background Service is stopping");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
/// <summary>
/// Disposes the Task
/// </summary>
public void Dispose()
{
_timer?.Dispose();
}
}
希望它也对您有帮助。