我如何订购asp.net core每天运行一次方法?

编程入门 行业动态 更新时间:2024-10-23 15:30:18
本文介绍了我如何订购asp core每天运行一次方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的asp核心程序需要收集前一天访问网站的所有IP。

My asp core program needs to collect all the IP which access the website pre-day.

我需要在以下情况下将IP列表输出到txt文件:一天结束了。

I need to output the IP list to a txt file when a day is over.

因此,程序应该运行一种每天保存一次IP列表的方法。

Therefore, the program should run a method to save the IP list once per-day.

我应该如何设计?

我认为,我将在 startup.cs 并将 Task.Delay 设置为24小时(一天)。

In my opinion, I will add a looping task in the startup.cs and set the Task.Delay to 24 hours(one day).

我应该这样做吗?谢谢。

Should I do it like this? Thank you.

推荐答案

这就是我在ASP.Net Core中实现调度程序任务的方式。请注意,您必须知道如何使用cron epxression。

This is how I implement scheduler task in ASP.Net Core. Please note that you have to know how to use cron epxression.

首先将此程序包添加到csproj中

First add this package in your csproj

<PackageReference Include="ncrontab" Version="3.3.1" />

接下来,我将创建SceduledProcessor.cs

Next I will create SceduledProcessor.cs

public abstract class ScheduledProcessor : ScopedProcessor { private readonly CrontabSchedule _schedule; private DateTime _nextRun; protected ScheduledProcessor(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory) { _schedule = CrontabSchedule.Parse(GetCronExpression(serviceScopeFactory)); _nextRun = _schedule.GetNextOccurrence(DateTime.Now); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { do { var now = DateTime.Now; _schedule.GetNextOccurrence(now); if (now > _nextRun) { await ProcessBackgroundTask(); _nextRun = _schedule.GetNextOccurrence(DateTime.Now); } await Task.Delay(5000, stoppingToken); } while (!stoppingToken.IsCancellationRequested); } protected abstract string GetCronExpression(IServiceScopeFactory serviceScopeFactory); }

将针对我们的方案运行ExecuteAsync

The ExecuteAsync will be run for our scenario

请注意,我们必须实现GetCronExpression方法

Please note that we have to implement GetCronExpression method

接下来,我将实现服务类

Next I will implement the service class

public class ScheduledEmailService : ScheduledProcessor { public ScheduledEmailService( IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory) { } /// <summary> /// Get cron setting from db /// </summary> /// <returns></returns> protected override string GetCronExpression(IServiceScopeFactory serviceScopeFactory) { return ""; // return your cron expression here you can get from db or static string } public override Task ProcessInScope(IServiceProvider serviceProvider) { // do something you wish return Task.CompletedTask; } }

这里的奖金我是小类,具有cron表达式的预定义值您可以使用它

Bonus here I small class with predefined value for cron expression you can use it

public static class CronExpression { public static readonly string EveryMinute = "0 * * * * *"; public static readonly string EveryDay = "0 0 0 * * *"; public static readonly string EveryWeek = "0 0 * * 0"; public static readonly string EveryWeekend = "0 0 0 * * 6,0"; }

最后将其添加到Startup.cs

Finally add this to your Startup.cs

services.AddSingleton<IHostedService, ScheduledEmailService>();

更多推荐

我如何订购asp.net core每天运行一次方法?

本文发布于:2023-11-22 05:33:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   asp   net   core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!