其他分享
首页 > 其他分享> > .Net Core之后台任务

.Net Core之后台任务

作者:互联网

.Net Core之后台任务

.Net Core自带的后台任务可以满足基础需求,而更高级的用法则需要自己实现或使用第三方库,比如Hangfire

自带后台任务(IHostedService)

一次性,周期性,任务队列都可以通过IHostedService快速实现,微软官方文档已经做了很详细的说明,我这里就放出一个周期任务的例子

    /// <summary>
    /// 定时任务
    /// </summary>
    public class TimedHostedService : IHostedService, IDisposable
    {
        private int executionCount = 0;
        private readonly ILogger<TimedHostedService> _logger;
        private Timer _timer;

        public TimedHostedService(ILogger<TimedHostedService> logger)
        {
            _logger = logger;
        }

        public Task StartAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Timed Hosted Service running.");

            //周期:一小时
            _timer = new Timer(DoWork, null, TimeSpan.Zero, 
                TimeSpan.FromHours(1));

            return Task.CompletedTask;
        }

        private void DoWork(object state)
        {
            var count = Interlocked.Increment(ref executionCount);
            
            //逻辑代码
            
            _logger.LogInformation(
                "Timed Hosted Service is working. Count: {Count}", count);
        }

        public Task StopAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Timed Hosted Service is stopping.");

            _timer?.Change(Timeout.Infinite, 0);

            return Task.CompletedTask;
        }

        public void Dispose()
        {
            _timer?.Dispose();
        }
    }
折叠

Hangfire

Hangfire在很多方面都做得很好,比起Quartz.NET,有web管理页面,集成和管理更方便,这里也不对两者进行比较

//ConfigureServices里注入
services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage(connectionString, new SqlServerStorageOptions
    {
        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
        QueuePollInterval = TimeSpan.Zero,
        UseRecommendedIsolationLevel = true,
        UsePageLocksOnDequeue = true,
        DisableGlobalLocks = true
    }));
//Configure里启用
//Hangfire管理页面
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
{
    Authorization = new[] { new HangFireAuthorizationFilter() }
});
app.UseHangfireServer();
/// <summary>
/// 定时任务
/// </summary>
public class JobService
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ILogger _logger;

    public JobService(IServiceProvider serviceProvider, ILogger<JobService> logger)
    {
        _serviceProvider = serviceProvider;
        _logger = logger;
    }
    
    public async Task DoWork()
    {
        _logger.LogInformation($"Start DoWork");
        using (var scope = _serviceProvider.CreateScope())
        {
            //逻辑代码
        }
        _logger.LogInformation($"End DoWork");
    }
}
services.AddSingleton<JobService>();
//后台任务:立刻执行
BackgroundJob.Enqueue(() => _jobService.DoWork());
//计划任务:1分钟后执行
BackgroundJob.Schedule(() => _jobService.DoWork(), TimeSpan.FromMinutes(1));
//周期任务:每小时执行一次
RecurringJob.AddOrUpdate(id, () => _jobService.DoWork(), Cron.Hourly);
//可以通过任务id取消任务
RecurringJob.RemoveIfExists(id);
转 https://www.cnblogs.com/shadowxs/p/14291011.html

标签:Core,Net,DoWork,Hangfire,public,任务,后台任务,logger
来源: https://www.cnblogs.com/wl-blog/p/16425649.html