我有一种情况,可扩展性至关重要。我有一个 API 端点,它必须调用第 3 方 Web 服务,并且可能需要 10 多秒才能完成。我担心的是 Web 请求在等待第 3 方请求完成时堆积在我们的服务器上。我需要确保对“StartJob”的请求立即返回,并且作业实际上在后台运行。最好的方法是什么?
// Client polls this endpoint to find out if job is complete
public ActionResult GetResults(int jobId)
{
return Content(Job.GetById(jobId));
}
//Client kicks off job with this endpoint
public ActionResult StartJob()
{
//Create a new job record
var job = new Job();
job.Save();
//start the job on a background thread and let IIS return it's current thread immediately
StartJob(); //????
return Content(job.Id);
}
//The job consists of calling a 3rd party web service which could take 10+ seconds.
private void StartJob(long jobId)
{
var client = new WebClient();
var response = client.downloadString("http://some3rdparty.com/dostuff");
var job = Job.GetById(jobId);
job.isComplete = true;
job.Save();
}