Gie*_*ius 7 c# asp.net-mvc iis-7.5 asp.net-mvc-3
我有奇怪的行为,无法在本地机器上复制,它开始让我疯狂.
貌似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))
}
Run Code Online (Sandbox Code Playgroud)
我用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
}
});
Run Code Online (Sandbox Code Playgroud)
对于小订单,它工作正常,但对于大订单,创建了两个订单,理由似乎是处理时间,订单越大,将其放置到外部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();
}
}
Run Code Online (Sandbox Code Playgroud)
而不是返回确认的数据,它返回"已经下订单".
我想也许是动作执行超时,但我只是为了清酒而尝试过
<httpRuntime executionTimeout="600" />
Run Code Online (Sandbox Code Playgroud)
没有帮助.
任何想法在哪里搜索原因,另外要检查什么,以启用任何其他日志记录?
更新:有趣的是,原始电话也已完成.
更新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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
从日志中只能从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);
}
Run Code Online (Sandbox Code Playgroud)
只要它在两个独立的开发机器(win 7)和ec2(win2008sp2)中的升级机器上运行正常,生产服务器IIS设置(win 2008R2 x64 sp1)几乎肯定是问题.
找到了。
正如我开始思考的那样,IIS 配置在某种程度上与此有关,我在生产 IIS 上启动了一个新的测试站点,只是默认的 mvc 站点,线程睡眠时间为 90 秒。
它按预期工作。
因此,我复制了整个生产站点以在不同的端口上运行,认为我将能够更快地测试想法而不影响生产站点 - 当它在不同的端口上运行时没有问题。
事实证明,我们使用的是 Amazon Elastic 平衡器,这就是问题所在。
道德是,如果我从一开始就积极地隔离问题,我会浪费更少的时间。
| 归档时间: |
|
| 查看次数: |
2071 次 |
| 最近记录: |