502经过长时间运行asp.net控制器actionresult

时间:2014-08-12 20:50:44

标签: asp.net asp.net-mvc

我有以下控制器ActionResult,我在其中调用prodManager.ProcessFile()。这需要2或3分钟,在此期间我将在浏览器中收到502错误,因为IIS超时。增加IIS超时所需的时间可能不是一个好主意。我如何修改它,或者在前一个视图中从AJAX调用它,然后在整个actionresult运行完成后显示一个状态,或者甚至让一些javascript调用一个不同的函数,这将给状态更新提供5秒左右的时间?

        [HttpPost]
        public ActionResult ProcessProducts(UploadViewModel model)
        {
            DateTime startTime = DateTime.UtcNow;
            DateTime endTime;
            TimeSpan totalTime;            
            PulProcessor prodManager = new PulProcessor();
            string inputFile = Path.Combine(Server.MapPath("~/files/incoming"), model.ProductsFileName);
            string outputPath = Server.MapPath("~/files/pul");

            prodManager.ProcessFile(inputFile, outputPath);

            endTime = DateTime.UtcNow;
            totalTime = endTime - startTime;

            ViewBag.StartTime = startTime.ToString();
            ViewBag.EndTime = endTime.ToString();
            ViewBag.TotalTime = totalTime.ToString();
            ViewBag.TotalOutput = prodManager.Products.Count.ToString();
            ViewBag.ProductCounter = prodManager.RecordsProcessed;
            ViewBag.FileName = prodManager.ProductsFileName;
            ViewBag.ExFileName = prodManager.ExceptionsFileName;
            ViewBag.Exceptions = prodManager.BelowCost.Count.ToString();

            return View();
        }

2 个答案:

答案 0 :(得分:0)

您可以尝试将处理逻辑封装到async task中。启动并将Task存储在静态“正在运行的任务”集合中,可能带有密钥。返回ActionResult以指示“已提交请求”。接下来,您可以通过AJAX“轮询”,并通过它的密钥获取Task的状态。

答案 1 :(得分:0)

我的控制器方法耗时太长而且超时。我解决了这个购买编写SignalR集线器以处理和输出我的文件,同时将操作状态报告给客户端上的状态栏。