Pet*_*ete 5 shopify asp.net-mvc-4 asp.net-web-api2
我已经为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));
}
}
Run Code Online (Sandbox Code Playgroud)
其中ProcessOrder循环遍历订单并将详细信息保存到我们的内部数据库.
但是,如果进程花费的时间太长,那么webhook会再次调用api,因为它认为它已经失败了.有没有办法先返回ok响应,然后再进行处理?
有点像在mvc控制器中返回重定向并且可以选择在重定向后继续处理其余操作.
请注意,我总是需要以Shopify的形式返回ok响应,如果它失败了19次,那么智慧决定删除webhook(并且处理时间过长会被视为失败)
Pet*_*ete 11
我设法通过使用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");
}
Run Code Online (Sandbox Code Playgroud)
有几个选项可以实现此目的:
Hangfire或Quartz运行实际的处理,您的 Web 请求将启动任务。RabbitMQ)来运行实际进程,而 Web 请求只是向队列添加一条消息...请注意,这可能是最好的,但可能需要一些重要的设置知识。Javascript AJAX在后台使用启动进程,也许您可以关闭该请求的重试...仍然使请求在后台运行,所以可能不完全是您喜欢的。| 归档时间: |
|
| 查看次数: |
6528 次 |
| 最近记录: |