我创建了一个 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 响应。
最佳答案
将工作委托(delegate)给其他进程。
它可以是您想要的任何东西,服务、命令行应用程序,任何合适的东西。
我会设置一个消息系统,让 webapi 将所有这些工作操作发送到一个队列,然后让一个 Windows 服务来处理这个队列。这将释放 api 的压力并让它处理其他请求,而无需等待长时间运行的任务完成。不必比这更复杂。
作为建议,RabbitMQ 相当简单,但我建议您环顾四周并选择您喜欢的任何内容
关于c# - 网络API : How can I push processing off so that the requester isn't hanging around for a response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40139176/