以下是一个示例解决方案,展示了如何使用ASP.NET Core 2.2 Web API创建一个队列后台任务:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
namespace WebApiQueueBackgroundTask
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// 注册后台任务服务
services.AddHostedService();
// 注册后台任务队列
services.AddSingleton();
// 添加日志记录
services.AddLogging();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebApiQueueBackgroundTask
{
public class QueueBackgroundTask : IQueueBackgroundTask
{
private readonly ILogger _logger;
private readonly IServiceProvider _serviceProvider;
public QueueBackgroundTask(ILogger logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}
public async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("队列后台任务已启动");
while (!stoppingToken.IsCancellationRequested)
{
using (var scope = _serviceProvider.CreateScope())
{
var scopedProcessingService =
scope.ServiceProvider
.GetRequiredService();
await scopedProcessingService.DoWork(stoppingToken);
}
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
_logger.LogInformation("队列后台任务已停止");
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace WebApiQueueBackgroundTask
{
public interface IScopedProcessingService
{
Task DoWork(CancellationToken stoppingToken);
}
public class ScopedProcessingService : IScopedProcessingService
{
private readonly ILogger _logger;
public ScopedProcessingService(ILogger logger)
{
_logger = logger;
}
public async Task DoWork(CancellationToken stoppingToken)
{
_logger.LogInformation("执行后台任务...");
// 执行你的后台任务逻辑
await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken);
_logger.LogInformation("后台任务执行完成");
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebApiQueueBackgroundTask
{
public class QueueBackgroundTaskService : BackgroundService
{
private readonly ILogger _logger;
private readonly IServiceProvider _serviceProvider;
public QueueBackgroundTaskService(ILogger logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("队列后台任务服务已启动");
await DoWork(stoppingToken);
}
private async Task DoWork(CancellationToken stoppingToken)
{
_logger.LogInformation("队列后台任务已启动");
while (!stoppingToken.IsCancellationRequested)
{
using (var scope = _serviceProvider.Create