微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 控制器操作被调用两次,IIS日志没有显示

我有奇怪的行为,无法在本地机器上复制,它开始让我疯狂.

看起来ASP.NET MVC正在尝试执行操作,某些超时,它会在没有任何异常的情况下失败并通知ajax客户端,然后尝试重新运行操作,ajax客户端获得响应,但不是来自原始调用.

我有一个控制器动作:

[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
                }
            });

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

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

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

为了确保,ajax客户端得到的响应不是来自原始调用我已经做了一些锁定:

[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();
     }
 }

而不是返回确认的数据,它返回“已经下订单”.

我想也许是动作执行超时,但我只是为了清酒而尝试过

<httpRuntime executionTimeout="600" />

没有帮助.

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

更新:有趣的是,原始电话也已完成.

更新2:我添加了动作过滤器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:

我在单独的测试控制器中将问题与简单的线程睡眠隔离开来:

[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

原文地址:https://www.jb51.cc/csharp/99649.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐