按照文档,我实现了一个库,允许我的 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 方法)还是系统负责释放分配给后台线程的资源?
编辑我正在添加实现方法的类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();
}
}