[英]How can I automatically call an ASP.NET Core endpoint each X hours?
I've build an API with ASP.NET Core.我用 ASP.NET Core 构建了一个 API。 Some of my controllers have an endpoint, which need to run each X hours.
我的一些控制器有一个端点,需要每 X 小时运行一次。 Nevertheless I still want to be able to call the endpoint by my own at any time.
尽管如此,我仍然希望能够随时自己调用端点。
The endpoints all have the same structure:端点都具有相同的结构:
[RequireHttps, HttpGet("/generate")]
public async Task<IActionResult> GenerateFiles()
{
// do stuff
}
Does anybody know how I could achieve this in ASP.NET Core itself without any third-party tool?有谁知道我如何在没有任何第三方工具的情况下在 ASP.NET Core 中实现这一点? I thought of using a timer, but I feel like this is not the best way to do so.
我想过使用计时器,但我觉得这不是最好的方法。 I highly appreciate any kind of help, cheers: :)
我非常感谢任何形式的帮助,干杯::)
With Hangfire that's quite easy to do:使用Hangfire这很容易做到:
Inside the Program.cs/Startup.cs:在 Program.cs/Startup.cs 中:
builder.Services
.AddHangfire(opts => opts
.UseSqlServerStorage(config
.GetSection(nameof(HangfireConfig))
.Get<HangfireConfig>()
.ConnectionString)
.AddUpdateJob(builder.Environment))
.AddHangfireServer()
And AddUpdateJob()
is implemented like this:而
AddUpdateJob()
是这样实现的:
public static IGlobalConfiguration AddUpdateJob(
this IGlobalConfiguration opts,
IWebHostEnvironment env)
{
if (!env.IsEnvironment("Local"))
{
RecurringJob.AddOrUpdate<IMasterDataUpdater>(
"masterdata-update",
x => x.Update(),
Cron.Hourly);
}
return otps;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.