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

标签 c# asp.net-web-api

我创建了一个 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/

相关文章:

c# - Global_Asax Application_Error Fired 而不是默认的 IIS7 404 Page Not Found Page

c# - 我错过了明显的吗?我想我将不得不在这里使用 ispostback 但不了解如何

c# - WebApi 中用于响应的 DelegatingHandler

asp.net - 在 MVC Controller 中将 JSON 绑定(bind)到 JToken

C# 上下文菜单处理程序

c# - Mono/C# : A good GUI toolkit ? 在 KDE 上看起来不错?

c# - LINQ/IEnumerable Skip().Take() 效率与 "yield return"一起使用

javascript - 无法绑定(bind)到 ng-repeat

c# - HttpClient vs HttpWebRequest 以获得更好的性能、安全性和更少的连接

c# - 此 Asp.Net WebAPI Controller 方法是否正确地从应用程序池中换出?