【发布时间】:2016-10-19 18:38:31
【问题描述】:
我创建了一个 WebAPI 方法,它将从发布到该方法的内容中插入数据到数据库中。
我的问题是我如何才能推迟处理,以便请求者不会等待响应?
例如:
[HttpPost]
[Route("MyTest/ImportData")]
public HttpResponseMessage AddData([FromBody] XElement xmlBody)
{
try
{
// Verify that I can read the XML..
// Push XML off to another process to insert into database.
UpdateDataBase(xmlBody);
// Notify requester that I accepted the file.
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
我有什么作品。但是,插入所有数据最多可能需要 4 分钟。我想验证我是否可以读取 xml,以某种方式关闭进程并给用户一个 OK 响应。
【问题讨论】:
-
另一种常见的做法是将您的数据发送到队列中,这样您的方法会很快返回,但监控队列的进程拥有它所需要的所有时间。
-
注释您应该返回 HTTP 202 Accepted 状态代码,而不是 200 OK。 202 表示工作尚未完成,但已被接受在某个时间完成。
标签: c# asp.net-web-api