我如何订购asp.net core每天运行一次方法? [英] How can I order asp.net core runs a method once per-day?
问题描述
我的asp.net核心程序需要收集前一天访问网站的所有IP。
My asp.net 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 $ c $中添加一个循环任务c>并将
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每天运行一次方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!