Web API 2返回OK响应,但在后台继续处理

 mobiledu2502859427 发布于 2022-12-10 11:22

我已经为shopify创建了一个mvc web api 2 webhook:

public class ShopifyController : ApiController
{
    // PUT: api/Afilliate/SaveOrder
    [ResponseType(typeof(string))]
    public IHttpActionResult WebHook(ShopifyOrder order)
    {
        // need to return 202 response otherwise webhook is deleted
        return Ok(ProcessOrder(order));
    }
}

其中ProcessOrder循环遍历订单并将详细信息保存到我们的内部数据库.

但是,如果进程花费的时间太长,那么webhook会再次调用api,因为它认为它已经失败了.有没有办法先返回ok响应,然后再进行处理?

有点像在mvc控制器中返回重定向并且可以选择在重定向后继续处理其余操作.

请注意,我总是需要以Shopify的形式返回ok响应,如果它失败了19次,那么智慧决定删除webhook(并且处理时间过长会被视为失败)

1 个回答
  • 我设法通过使用Task以下方式异步运行处理来解决我的问题:

        // PUT: api/Afilliate/SaveOrder
        public IHttpActionResult WebHook(ShopifyOrder order)
        {
            // this should process the order asynchronously
            var tasks = new[]
            {
                Task.Run(() => ProcessOrder(order))
            };
    
            // without the await here, this should be hit before the order processing is complete
            return Ok("ok");
        }
    

    2022-12-11 02:10 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有