我已经为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(并且处理时间过长会被视为失败)
我设法通过使用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"); }