Ajax http 500 错误 Azure 不在本地

Ajax http 500 error Azure not on local

当 运行 发出 ajax 请求时,我收到一条错误消息:

Failed to load resource: the server responded with a status of 500 (OK)

问题是服务器错误似乎并没有发生。当我 运行 本地计算机上的应用程序但使用 azure 数据库时,我没有收到错误消息。只有发布的 Azure 应用程序才会生成此错误。我已经完成了远程调试,即使浏览器显示此错误,服务器仍会继续处理请求,并在几分钟后完成请求。好像真的没有服务器错误。

服务器需要大约 10 分钟来完成请求。我相信这与请求很长这一事实有关(因为它适用于较小的数据库)。我知道 azure 对 CPU 免费应用服务级别的时间有限制,但我切换到基本(没有 cpu 时间限制)所以这应该不是问题。请求非常 sql 强烈(大约 20k sql 查询)。

Ajax 通话:

  $.ajax({
  async: true,
  cache: false,
  type: "POST",
  url: FooURL,
  contentType: 'application/json',
  dataType: "json",
  data: JSON.stringify(null),
  success: function (error_message) {
    $("#FooBar").removeClass("loading");
  },
  error: function(jqXHR, textStatus, errorThrown) {
  console.log(textStatus, errorThrown);
  }
});

控制器:

  [Authorize]
  [RequireHttpsAttribute]
  public class FooController : Controller
  {
    private FooBarModel foobarModel = new FooBarModel();

    public ActionResult UpdateFooBarModel()
    {
      foobarModel.UpdateModel();
      return Json("Success");
    }

Azure 中显然存在空闲超时,here 对此进行了描述。默认值设置为 4 分钟,如果您 运行 您的应用程序在 VM 上,则最多可以配置为 30 分钟。我通过在数据库中创建一个 table 来解决它,其中存储了请求的当前状态。

Table:

CREATE TABLE [dbo].[MyTable] (
[UpdateNo] INT IDENTITY (1, 1) NOT NULL,
[AllDone]  BIT DEFAULT ((0)) NOT NULL,
CONSTRAINT [MyTable$PrimaryKey] PRIMARY KEY CLUSTERED ([UpdateNo] ASC)

);

我没有直接调用方法,而是创建了一个任务和 returns 更新状态行的 ID。

public ActionResult UpdateFooBarModel()
{
  int id = foobarModel.StartUpUpdate(); //Creates the status row 
  Task.Run(() => foobarModel.UpdateModel(id));
  return Json(id);
}

public ActionResult GetUpdateStatus(int UpdateNo)
{
  bool status = foobarModel.GetUpdateStatus(UpdateNo);
  return Json(status);
}

Ajax 通话:

function check_status(StatusId) {
$.ajax({
  async: true,
  cache: false,
  type: "POST",
  url: GetUpdateStatusURL + "/?UpdateNo=" + StatusId,
  contentType: 'application/json',
  dataType: "json",
  success: function (StatusDone) {
    if (StatusDone == true) {
      console.log("Update done!");
      $("#FooBar").removeClass("loading");
    } else {
      console.log("Not done!")
      setTimeout(function () {
        check_status(StatusId);
      }, 5000); //Check every 5 seconds
    }
  },
  error: function (jqXHR, textStatus, errorThrown) {
    console.log(textStatus, errorThrown);
  }
});

}