gpt4 book ai didi

c# - Controller 操作被调用两次并且 IIS 日志不显示

转载 作者:太空狗 更新时间:2023-10-29 20:42:07 25 4
gpt4 key购买 nike

我有奇怪的行为,无法在本地机器上复制,这让我抓狂。

看起来 ASP.NET MVC 正在尝试执行操作,超时,它毫无异常(exception)地失败并通知 ajax 客户端,然后尝试重新运行操作,ajax 客户端获得响应,但不是来自原始调用。

我有一个 Controller Action :

 [ValidateAntiForgeryToken]
[ClientErrorHandler]
public virtual ActionResult PlaceOrder(CheckoutDto checkoutDto)
{
LoadDataFromDatabase();

// this may take up to couple minutes
var orderConfirmationData = PlaceOrderToExternalWebservice();

SaveConfirmationData(orderConfirmationData);

return View(Transform(orderConfirmationData))
}

我使用 jquery ajax 调用它:

$.ajax({
url: placeOrderActionUrl,
type: "POST",
async: true,
dataType: "html",
data: $('#checkoutForm').serialize(),
success: function (data) {
// show confirmation data
},
error: function (request, status, error) {
// show error message
}
});

对于小订单,它工作正常,但对于大订单,会创建两个订单,原因似乎是处理时间,订单越大,将其放置到外部网络服务所花费的时间就越多。

我检查了 IIS 日志,以确保客户端脚本没有调用操作两次 - IIS 日志只显示对特定操作的一次调用。

外部服务没有失败,事件日志/sql日志中没有记录异常。

为了确保 ajax 客户端得到的响应不是来 self 已经做了某种锁定的原始调用:

 [ValidateAntiForgeryToken]
[ClientErrorHandler]
public virtual ActionResult PlaceOrder(CheckoutDto checkoutDto)
{
try
{
if (OrderingLockedForCurrentUser())
{
Log("Locked");
return View("Already placing order");
}

LockOrderingForCurrentUser();

LoadDataFromDatabase();

// this may take up to couple minutes
var orderConfirmationData = PlaceOrderToExternalWebservice();

SaveConfirmationData(orderConfirmationData);

return View(Transform(orderConfirmationData))
}
finally
{
RemoveOrderingLockForCurrentUser();
}
}

并且它没有返回确认数据,而是返回“已经下订单”。

我想过也许 Action 执行超时,但我试过只是为了缘故

<httpRuntime executionTimeout="600" />

没有帮助。

任何想法在哪里搜索原因,另外要检查什么,以启用任何额外的日志记录?

更新:有趣的是,原来的调用也完成了。

更新 2:我添加了 Action 过滤器 AjaxOnly 以确保它只能从 javascript 调用:

public class AjaxOnlyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
throw new Exception("This action is intended to be called from Ajax only.");
}
}
}

并且从日志来看,它仅从 javascript 调用,所以谜团还在继续......

更新 3:

我已将问题隔离到单独的测试 Controller 中的简单线程 sleep :

[ValidateAntiForgeryToken]
[AjaxOnly]
[ClientErrorHandler]
public virtual ActionResult PlaceOrderAction(CheckoutDto checkoutDto)
{
try
{
if (CanPlaceOrder(Request.RequestContext.HttpContext))
{
Thread.Sleep(TimeSpan.FromSeconds(90));

return Content("First time");
}

return Content("Second time");
}
finally
{
HttpContext.Cache.Remove(GetKey(userService.CurrentUser.UserId));
}
}

public bool CanPlaceOrder(HttpContextBase httpContext)
{
var userId = userService.CurrentUser.UserId;
var key = GetKey(userId);

if (httpContext.Cache[key] == null)
{
httpContext.Cache.Add(key, userId, null, DateTime.Now.AddMinutes(10), new TimeSpan(), CacheItemPriority.High, null);
return true;
}

return false;
}

private static string GetKey(int userId)
{
return "PlacingOrder{0}".With(userId);
}

只要它在两台独立的开发机器 (win 7) 和 ec2 (win2008sp2) 中的临时机器上运行正常,几乎可以肯定是生产服务器 IIS 设置 (win 2008R2 x64 sp1) 有问题。

最佳答案

这是来自类似问题的建议:

“是否有任何其他标记可能会意外引用页面?脚本引用、图像引用、css 引用,都可能被错误地指向 '.'。或当前页面。”

MVC controller is being called twice

关于c# - Controller 操作被调用两次并且 IIS 日志不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11575885/

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