在docs之后,我实现了一个库,该库允许我的Web api在后台创建和运行任务。基本上,我要做的是创建一个QueuedHostedService
(继承自BackgroundService
),其中包含要完成的任务并将其添加到队列中。然后,通过同时运行此任务(我想在不同的线程上)来逐步清空此队列。以下是BackgroundService
public abstract class BackgroundService : IHostedService, IDisposable
{
protected BackgroundService();
public virtual void Dispose();
//
// Summary:
// Triggered when the application host is ready to start the service.
//
// Parameters:
// cancellationToken:
// Indicates that the start process has been aborted.
public virtual Task StartAsync(CancellationToken cancellationToken);
//
// Summary:
// Triggered when the application host is performing a graceful shutdown.
//
// Parameters:
// cancellationToken:
// Indicates that the shutdown process should no longer be graceful.
[AsyncStateMachine(typeof(<StopAsync>d__4))]
public virtual Task StopAsync(CancellationToken cancellationToken);
//
// Summary:
// This method is called when the Microsoft.Extensions.Hosting.IHostedService starts.
// The implementation should return a task that represents the lifetime of the long
// running operation(s) being performed.
//
// Parameters:
// stoppingToken:
// Triggered when Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)
// is called.
//
// Returns:
// A System.Threading.Tasks.Task that represents the long running operations.
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
我不知道的是,Task
完成后会发生什么?我是否需要做一些事情(例如使用Dispose方法),或者系统是否负责释放分配给后台线程的资源?
EDIT (我要添加类QueuedHostedService
的实现,该类实现了ExecuteAsinc()
方法
public class QueuedHostedService : BackgroundService
{
private static ILog logger = LogExtensions.GetClassLogger();
public QueuedHostedService(IBackgroundTaskQueue taskQueue)
{
TaskQueue = taskQueue;
}
public IBackgroundTaskQueue TaskQueue { get; }
protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{
logger.MethodEnter();
while (!cancellationToken.IsCancellationRequested)
{
var workItem = await TaskQueue.DequeueAsync(cancellationToken);
try
{
await workItem(cancellationToken);
}
catch (Exception ex)
{
logger.Error($"Error occurred executing {nameof(workItem)}. with the following exception {ex.Message}");
}
}
logger.MethodLeave();
}
}