title: Azure Websites - 502 - Web server received an invalid response while acting as a gateway or proxy server
body: <p>My web app is running fine on my local machine but when published to a Windows Azure Website, I get the following error on one of the controller action methods:</p>
  
  <p>502 - Web server received an invalid response while acting as a gateway or proxy server.
  There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.</p>
  
  <p>I used Elmah to capture more information and I get:
  System.ArgumentNullException
  Value cannot be null. Parameter name: path2</p>
  
  <p>So it's referencing when I use path.combine(path1, path2) in my method. I can't figure out what is going on, the input file is being read fine and the output files are being generated fine when I check the output directory. </p>
  
  <p>Any other suggestions on what might be going on?</p>
  
  <p>Here is the code for my action method:</p>
  
  <pre><code>        [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&lt;PulMap&gt;();
                      List&lt;PulProduct&gt; prodList = csv.GetRecords&lt;PulProduct&gt;().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&lt;PulFinalProduct&gt; 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);
          }
      }
  </code></pre>
  
  <p>Here is what the Azure Eventlog gives me:</p>
  
  <pre><code>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.&lt;BeginInvokeSynchronousActionMethod&gt;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.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass45.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3e() at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass30.&lt;BeginInvokeActionMethodWithFilters&gt;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.&lt;&gt;c__DisplayClass1e.&lt;&gt;c__DisplayClass28.&lt;BeginInvokeAction&gt;b__19() at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass1e.&lt;BeginInvokeAction&gt;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.&lt;BeginExecuteCore&gt;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.&lt;BeginExecute&gt;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.&lt;BeginProcessRequest&gt;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&amp; completedSynchronously) 
  </code></pre>
  
  <p>OK, I was able to do remote debugging on this azure site. The code inside my controller action is running fine. After I get the original 502 error, the controller continues to process the input data and then write the output files. Everything works fine and the only problem I'm having is getting a 502 page while I am waiting for it to process and load the next view. Does a controller have a certain amount of time to return a view and if it doesn't is this why I'm getting the 502 error?</p>
  
  <p>EDIT: I'm pretty sure what I'm experiencing is a timeout error because my controller method takes 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 timeout settings on the server so maybe there something using ajax that can be done like sending a status update to the browser after every so often. I'm totally new to this so I'll have to do some research.</p>