![](/img/trans.png)
[英]ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get request
[英]How can I automatically call an ASP.NET Core endpoint each X hours?
我用 ASP.NET Core 構建了一個 API。 我的一些控制器有一個端點,需要每 X 小時運行一次。 盡管如此,我仍然希望能夠隨時自己調用端點。
端點都具有相同的結構:
[RequireHttps, HttpGet("/generate")]
public async Task<IActionResult> GenerateFiles()
{
// do stuff
}
有誰知道我如何在沒有任何第三方工具的情況下在 ASP.NET Core 中實現這一點? 我想過使用計時器,但我覺得這不是最好的方法。 我非常感謝任何形式的幫助,干杯::)
使用Hangfire這很容易做到:
在 Program.cs/Startup.cs 中:
builder.Services
.AddHangfire(opts => opts
.UseSqlServerStorage(config
.GetSection(nameof(HangfireConfig))
.Get<HangfireConfig>()
.ConnectionString)
.AddUpdateJob(builder.Environment))
.AddHangfireServer()
而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.