gpt4 book ai didi

c# - WebAPI : How can I push processing off so that the requester isn't hanging around for a response?

转载 作者:行者123 更新时间:2023-11-30 23:19:42 24 4
gpt4 key购买 nike

我创建了一个 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# - WebAPI : How can I push processing off so that the requester isn't hanging around for a response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40139176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com