0
我無法讓FluentScheduler在.Net Framework 4.5.2 Web api中工作。幾天前,我問了一個關於通過控制檯應用程序進行調度的類似問題,並且可以讓它與幫助一起工作,但不幸的是現在面臨Web Api的問題。以下是代碼。如何使用Web API使用FluentScheduler庫來安排作業?
[HttpPost]
[Route("Schedule")]
public IHttpActionResult Schedule([FromBody] SchedulerModel schedulerModel)
{
var registry = new Registry();
registry.Schedule<MyJob>().ToRunNow();
JobManager.Initialize(registry);
JobManager.StopAndBlock();
return Json(new { success = true, message = "Scheduled!" });
}
下面是我要安排這對於現在只是寫文本文件
public class SampleJob: IJob, IRegisteredObject
{
private readonly object _lock = new object();
private bool _shuttingDown;
public SampleJob()
{
HostingEnvironment.RegisterObject(this);
}
public void Execute()
{
lock (_lock)
{
if (_shuttingDown)
return;
//Schedule writing to a text file
WriteToFile();
}
}
public void WriteToFile()
{
string text = "Random text";
File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
}
public void Stop(bool immediate)
{
lock (_lock)
{
_shuttingDown = true;
}
HostingEnvironment.UnregisterObject(this);
}
它可以在本地主機上運行嗎? – coder771