在我的 Web 应用程序中,我有一个执行时间较长的任务,我想在后台调用这个任务。因此,根据文档 .NET Core 3.1 队列后台任务,我使用以下代码:
public interface IBackgroundTaskQueue
{
ValueTask QueueBackgroundWorkItemAsync(Func<CancellationToken, ValueTask> workItem);
ValueTask<Func<CancellationToken, ValueTask>> DequeueAsync(CancellationToken cancellationToken);
}
public class BackgroundTaskQueue : IBackgroundTaskQueue
{
private readonly Channel<Func<CancellationToken, ValueTask>> _queue;
public BackgroundTaskQueue(int capacity)
{
var options = new BoundedChannelOptions(capacity){FullMode = BoundedChannelFullMode.Wait};
_queue = Channel.CreateBounded<Func<CancellationToken, ValueTask>>(options);
}
public async ValueTask QueueBackgroundWorkItemAsync(Func<CancellationToken, ValueTask> workItem)
{
if (workItem == null)throw new ArgumentNullException(nameof(workItem));
await _queue.Writer.WriteAsync(workItem);
}
public async ValueTask<Func<CancellationToken, ValueTask>> DequeueAsync(CancellationToken cancellationToken)
{
var workItem = await _queue.Reader.ReadAsync(cancellationToken);
return workItem;
}
}
托管服务
public class QueuedHostedService : BackgroundService
{
private readonly ILogger<QueuedHostedService> _logger;
public QueuedHostedService(IBackgroundTaskQueue taskQueue, ILogger<QueuedHostedService> logger)
{
TaskQueue = taskQueue;
_logger = logger;
}
public IBackgroundTaskQueue TaskQueue { get; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await BackgroundProcessing(stoppingToken);
}
private async Task BackgroundProcessing(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var workItem = await TaskQueue.DequeueAsync(stoppingToken);
try
{
await workItem(stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred executing {WorkItem}.", nameof(workItem));
}
}
}
public override async Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Queued Hosted Service is stopping.");
await base.StopAsync(stoppingToken);
}
}
然后我注册所有服务
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue>(new BackgroundTaskQueue(queueCapacity));
然后我可以通过不带参数的调用方式成功使用它,就像这个示例中一样。
public async Task<TenantBo> RegisterCompanyAsync(AddTenantBo addTenantBo)
{
var tenantBo = new TenantBo();
try
{
_companyRegistrationLogHelper.SetInfoLog(GetTenantId(tenantBo),
"Start create company: " + JsonConvert.SerializeObject(addTenantBo));
InitOnCreateCompanyTasks(tenantBo);
//skip if already create tenant
tenantBo = await CreateTenantAsync(tenantBo, addTenantBo);
//run in background
_companyRegistationQueue.QueueBackgroundWorkItemAsync(RunRegistrationCompanyMainAsync);
return tenantBo;
}
catch (Exception e)
{
//some logs
return tenantBo;
}
}
private async ValueTask RunRegistrationCompanyMainAsync(CancellationToken cancellationToken)
{
//some await Tasks
}
private async ValueTask RunRegistrationCompanyMainAsync(string tenantId, CancellationToken cancellationToken)
{
//some await Tasks
}
因此,我只能使用一个参数来调用RunRegistrationCompanyMainAsync(CancellationToken cancellationToken),而无法使用两个参数的RunRegistrationCompanyMainAsync(string tenantId, CancellationToken cancellationToken)。
您能帮助我将字符串参数作为此任务的参数传递吗?
Func<string, CancellationToken, ValueTask>
,您可以像对无参数作业一样使用相同的Func<CancellationToken, ValueTask>
。 您的Tuple
解决方案与编译器实际上在我的解决方案中使用闭包的方式几乎相同,但更麻烦。 - Guru Stron