title: ASP.NET 5 (RC1) Bad Gateway: The specified CGI application encountered an error and the server terminated the process
body: <p>I am running an ASP.NET 5 project in an Azure Web App.</p>
  
  <p>When calling on an API endpoint with a file (form-data) of about 1.5mb or larger, I get a <code>502 Bad Gateway</code> with the follow message in the reponse body:</p>
  
  <blockquote>
    <p>The specified CGI application encountered an error and the server
    terminated the process.</p>
  </blockquote>
  
  <p>The strange thing is that when uploading a smaller file, the call works fine. It seems to produce the <code>502</code> at around the 1.5mb mark, but it's not entirely consistent.</p>
  
  <p>I am using ASP.NET 5 RC1.</p>
  
  <p>In Startup.cs, I have <code>app.UseIISPlatformHandler();</code> as the first middleware addition in the <code>Configure()</code> method.</p>
  
  <p><strong><code>project.config:</code></strong></p>
  
  <pre><code>{
    ...
    "webroot": "wwwroot",
    "dependencies": {
      "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
      "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
      "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
      "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
      "Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
      "Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final",
      "Microsoft.AspNet.Mvc.Abstractions": "6.0.0-rc1-final",
      "Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc1-final",
      "Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final",
      "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
      "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
      "Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",
      "Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
      "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
      "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
      "Newtonsoft.Json": "6.0.6",
      "WindowsAzure.Storage": "5.0.2",
      "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
      ...
    },
    "commands": {
      "web": "Microsoft.AspNet.Server.Kestrel"
    },
    "frameworks": {
      "dnx451": { }
    },
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As suggested by Jessevl, the following exception is being thrown:</p>
  
  <blockquote>
    <p>System.IO.InvalidDataException: Unexpected end of request content at
    Microsoft.AspNet.Server.Kestrel.Http.MessageBody.ForContentLength.d__3.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.WebUtilities.FileBufferingReadStream.d__27.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.WebUtilities.BufferedReadStream.d__39.MoveNext() ---
    End of stack trace from previous location where exception was thrown
    --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.WebUtilities.MultipartReaderStream.d__36.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.WebUtilities.StreamHelperExtensions.d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task
    task) at
    Microsoft.AspNet.Http.Features.Internal.FormFeature.d__12.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Mvc.ModelBinding.FormValueProviderFactory.d__1.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Mvc.ModelBinding.CompositeValueProvider.d__2.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task
    task) at
    Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__49.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.d__10.MoveNext() ---
    End of stack trace from previous location where exception was thrown
    --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at Microsoft.AspNet.Routing.RouteCollection.d__9.MoveNext() ---
    End of stack trace from previous location where exception was thrown
    --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveNext() ---
    End of stack trace from previous location where exception was thrown
    --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.ApplicationInsights.AspNet.RequestTrackingMiddleware.d__4.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Hosting.Internal.RequestServicesContainerMiddleware.d__3.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at
    Microsoft.AspNet.Hosting.Internal.HostingEngine.&lt;>c__DisplayClass32_0.d.MoveNext()
    --- End of stack trace from previous location where exception was thrown --- at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task) at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task) at Microsoft.AspNet.Server.Kestrel.Http.Frame.d__79.MoveNext()</p>
  </blockquote>