我是 C# ASP.NET Core 新手。
我现在使用的表单带有一个提交按钮
[HttpPost]
。[HttpPost]
的方法,而无需单击或打开任何内容。
我搜索并找到了这段代码,但没有用。
bool _ran = false; //initial setting at start up
private void timer_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Hour == 11 && _ran == false)
{
_ran = true;
(action to do)
}
if (DateTime.Now.Hour != 7 && _ran == true)
{
_ran = false;
}
}
您可以使用 Quartz.NET 作为调度程序。
例如,这是 ASP.NET Core 应用程序中的最小设置,其中每分钟执行一次操作(写入控制台)(当秒为零时):
Program.cs
文件:
using Quartz;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddQuartz(q =>
{
var jobKey = new JobKey("MyJobKey");
q.AddJob<MyJob>(opts => opts.WithIdentity(jobKey));
q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity("MyJob-trigger")
.WithCronSchedule("0 * * ? * *") // Use a CRON expression here. This particular one can be described as "run every minute" (when second is zero)
);
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
WebApplication app = builder.Build();
app.Run();
class MyJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine(DateTime.UtcNow); // Do anything you need here
return Task.FromResult(true); //Note: This method must always return a value. This is especially important for trigger listers watching job execution
}
}
只需将
Console.WriteLine(DateTime.UtcNow)
替换为您想要执行的任何实际代码,并更新 "0 * * ? * *"
CRON 表达式即可在需要时执行此操作(您可以使用此 帮助程序页面 创建满足您需求的 CRON 表达式)。
我是 c# asp.net core 的新手,我现在使用一个带有按钮的表单, 提交[httppost]。我想要一种自动发布 [httppost] 的方法 服务器上的特定时间,无需单击或打开任何内容。
嗯,您可以使用 PeriodicTimer 以便按特定计划执行任何操作。周期性定时器有一个名为 WaitForNextTickAsync 的方法,它将等待下一次自动执行。
除此之外,您甚至还可以使用其他几个服务来做到这一点。例如 Windows Worker Service 或 TaskScheduler。但是,这取决于您的场景和要求。对于简单的调度,您可以尝试使用PeriodicTimer,但如果您有特定要求,您可以尝试工作人员服务。
但一般来说,你可以尝试一下PeriodicTimer。让我们看看如何实现:
var timer = new PeriodicTimer(TimeSpan.FromSeconds(5));
int counter = 0;
while (await timer.WaitForNextTickAsync())
{
counter++;
if (counter > 5) break;
ExecutePostRequestTimer(counter).Wait();
}
static async Task ExecutePostRequestTimer(int counter)
{
HttpClient _httpClient = new HttpClient();
UpdateAddressModel modeldata = new UpdateAddressModel();
modeldata.Email = "[email protected]";
string json = JsonSerializer.Serialize(modeldata);
HttpResponseMessage response = await _httpClient.PostAsJsonAsync("http://localhost:5094/api/User/EditAddress", json);
if (response.IsSuccessStatusCode)
{
}
Console.WriteLine("Current counter: {0} Last Fired At: {1}, response Status: {2}", counter, DateTime.Now, response);
}
注意:我已经使用控制台应用程序和asp.net core API应用程序进行了测试。您可以使用合适的应用程序来做到这一点。请参阅此官方文档了解更多详细信息。
类似这样的:
var task = Task.Run(async () => {
var waitFor = DateTime.Now.Date.AddHours(23).AddMinutes(7) - DateTime.Now;
await Task.Delay(waitFor).ConfigureAwait(false);
using var client = new HttpClient();
var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(data);
var content = new StringContent(requestJson, encoding: System.Text.Encoding.UTF8, "application/json");
await client.PostAsync(url,content).ConfigureAwait(false);
}, TaskCreationOptions.LongRunning);