Azure Websites - 502 - The web server received an invalid response acting as a gateway or proxy

My web app works fine on my local machine, but when publishing to a Windows Azure website I get the following error on one of the controller action methods:

502 - The web server received an invalid response while acting as a gateway or proxy. There is a problem with the page you are looking for and it cannot be displayed. When the web server (acting as a gateway or proxy) contacted the content server upstream, it received an invalid response from the content server.

I used Elmah to collect more information and I get: System.ArgumentNullException The value cannot be null. Parameter name: path2

So it is referenced when I use path.combine (path1, path2) in my method. I can't figure out what's going on, the input file reads fine and the output files are generated fine when I check the output directory.

Any other suggestions on what might be going on?

Here is the code for my action:

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

      

This is what the Azure Eventlog gives me:

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) 

      

OK, I was able to do remote debugging on this azure site. The code inside my controller action is working fine. After receiving the original 502 error, the controller continues to process the input and then writes the output files. Everything works fine and the only problem I have is getting the 502 page while I wait for it to process and load the next view. The controller has a certain amount of time to return the view, and if it doesn't, then why am I getting a 502 error?

EDIT: I'm pretty sure I'm experiencing a timeout error because my controller method is too long to run. I probably can't speed up the controller method, so I'm not sure what I can do. I don't think I can or should change the timeout setting on the server, so maybe there is something using ajax that can be done like sending a status update to the browser after every so often. I'm completely new to this, so I'll have to do some research.

+3


source to share


1 answer


It turned out that the whole subroutine was running for too long and therefore dropped, hence the 502 error. I solved this by rewriting the class to use SignalR to run a task to create the file, and then update the user with the progress that was made.



+1


source







All Articles