Azure网站 - 502 - Web服务器在充当网关或代理服务器时收到无效响应

时间:2014-08-08 00:14:09

标签: c# azure azure-web-sites

我的网络应用在我的本地计算机上正常运行但是当发布到Windows Azure网站时,我在其中一个控制器操作方法上遇到以下错误:

502 - Web服务器在充当网关或代理服务器时收到无效响应。 您正在查找的页面存在问题,无法显示。当Web服务器(作为网关或代理)与上游内容服务器联系时,它从内容服务器收到无效响应。

我使用Elmah捕获更多信息,我得到: System.ArgumentNullException 值不能为空。参数名称:path2

所以当我在我的方法中使用path.combine(path1,path2)时它会引用。我无法弄清楚发生了什么,输入文件被正确读取,并且在检查输出目录时输出文件正常生成。

有关可能发生的事情的任何其他建议?

以下是我的操作方法的代码:

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

            try
            {                
                using (TextReader prodFile = System.IO.File.OpenText(filePath))
                {
                    CsvReader csv = new CsvReader(prodFile);
                    // map is at end of this file
                    csv.Configuration.RegisterClassMap<PulMap>();
                    List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();

                    foreach (var product in prodList)
                    {
                        prodManager.ProcessProduct(product);
                    }
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
            string currentDate = DateTime.UtcNow.ToString("yyyyMMdd");
            string productFileName = "PUL" + currentDate + ".txt";
            string exceptionsFileName = "PUL" + currentDate + "belowcost.txt";

            WriteFile(prodManager.Products, productFileName);
            WriteFile(prodManager.BelowCost, exceptionsFileName);

            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 = productFileName;
            ViewBag.ExFileName = exceptionsFileName;
            ViewBag.Exceptions = prodManager.BelowCost.Count.ToString();

            return View();
        }

    private void WriteFile(List<PulFinalProduct> prodList, string fileName)
    {
        try
        {
            string filePath = Path.Combine(Server.MapPath("~/files/pul"), fileName);
            StreamWriter writer = new StreamWriter(filePath, false);
            StringBuilder fileHeader = new StringBuilder();

            fileHeader.Append("Inventory Number\t");
            fileHeader.Append("MPN\t");
            fileHeader.Append("Retail Price\t");
            fileHeader.Append("Seller Cost\t");
            fileHeader.Append("Buy It Now Price\t");
            fileHeader.Append("Starting Bid\t");
            fileHeader.Append("ChannelAdvisor Store Price\t");
            fileHeader.Append("Quantity\t");
            fileHeader.Append("Quantity Update Type\t");
            fileHeader.Append("UPC\t");
            fileHeader.Append("Weight\t");
            fileHeader.Append("Brand\t");
            fileHeader.Append("Manufacturer\t");

            using (writer)
            {
                writer.WriteLine(fileHeader.ToString());

                foreach (var product in prodList)
                {
                    StringBuilder productLine = new StringBuilder();
                    productLine.Append(product.InventoryNumber + "\t");
                    productLine.Append(product.MPN + "\t");
                    productLine.Append(product.RetailPrice.ToString() + "\t");
                    productLine.Append(product.SellerCost.ToString() + "\t");
                    productLine.Append(product.BINPrice.ToString() + "\t");
                    productLine.Append(product.StartingBid.ToString() + "\t");
                    productLine.Append(product.CAStorePrice + "\t");
                    productLine.Append(product.Quantity + "\t");
                    productLine.Append(product.QUType + "\t");
                    productLine.Append(product.UPC + "\t");
                    productLine.Append(product.Weight + "\t");
                    productLine.Append(product.Brand + "\t");
                    productLine.Append(product.Manufacturer + "\t");

                    writer.WriteLine(productLine.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

以下是Azure Eventlog给我的内容:

Value cannot be null. Parameter name: path2 at System.IO.Path.Combine(String path1, String path2) at Jemco.Web.Controllers.PartsUnlimitedController.ProcessProducts(UploadViewModel model) at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<>c__DisplayClass28.<BeginInvokeAction>b__19() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

好的,我能够在这个天蓝色的网站上进行远程调试。我的控制器动作中的代码运行正常。在我得到原始502错误后,控制器继续处理输入数据,然后写入输出文件。一切正常,我遇到的唯一问题就是在等待处理和加载下一个视图的同时获得502页面。控制器是否有一定的时间来返回视图,如果不是这就是为什么我会收到502错误?

编辑:我很确定我遇到的是超时错误,因为我的控制器方法运行时间太长。我可能无法加速控制器方法,所以我不知道我能做什么。我不认为我可以或应该更改服务器上的超时设置,所以可能有一些使用ajax可以做的事情,比如每隔一段时间就将状态更新发送到浏览器。我对此完全陌生,所以我将不得不做一些研究。

1 个答案:

答案 0 :(得分:1)

事实证明,整个子程序运行时间太长而无法运行,因此超时因为502错误。我通过重写类来使用SignalR来运行任务来创建文件然后更新用户关于正在进行的进度来解决这个问题。