title: Request with size over 64KB fails to complete (Asp.Net Core Web API)
body: <p>I'm developing a REST web service using Asp.Net Core 2.0 Web API technology (full .Net Framework 4.6.1) within Visual Studio 2017 (15.6.5).</p>
  
  <h2>My issue</h2>
  
  <p>Whenever I send a post request that exceeds 64 KB, it hangs and never completes. For example, a post request with size of 68'259 bytes fails whereas one with a size of 63'534 bytes does complete without any problems.</p>
  
  <p>While the issue first started appearing when I tried to upload images (using <code>MultipartFormDataContent</code>), the content of the request does not matter. I also tried to send the image both as a byte array and as a converted Base64 string inside of a property within another object instance.</p>
  
  <h2>What exactly happens</h2>
  
  <p>When capturing traffic with Fiddler, the request never finishes. It never receives a response.</p>
  
  <p><img src="https://i.stack.imgur.com/9Ex5C.png" alt="Request log entry"></p>
  
  <p><img src="https://i.stack.imgur.com/GJP0o.png" alt="Request details"></p>
  
  <p>The controller method is never even called, as no breakpoint within it is ever hit. However, the Invoke method of our custom middleware is getting called, though it never runs past the line <code>await this._next.Invoke(context)</code>.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class VersioningMiddleware
  {
      private readonly RequestDelegate _next;
  
      public VersioningMiddleware(RequestDelegate next)
      {
          this._next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          ...
  
          await this._next.Invoke(context);
      }
      ...
  }
  </code></pre>
  
  <h2>Further information</h2>
  
  <p>I used <a href="https://ilclubdellesei.wordpress.com/2018/02/14/how-to-upload-images-to-an-asp-net-core-rest-service-with-xamarin-forms/" rel="nofollow noreferrer">this tutorial</a> for uploading images.</p>
  
  <p>Upon trying to figure out (to no avail) why the webservice never returns a response, I downloaded the example solution to see if that one even works. And it did, though it uses .Net Standard instead of the full .Net Framework, so I created a new webservice project within that solution using the full .Net Framework, copied over the whole controller and also changed the target platform to x86 to match the configuration to my actual web service (we still have to query MS Access databases, which is why I’m stuck with a 32 bit application).
  This new version of the example web service also worked like a charm for uploading images bigger than 64 KB.</p>
  
  <p>This didn’t make much sense to me, since I couldn’t see any significant differences.</p>
  
  <p><strong>App code sending the request (abbreviated in some parts for clarity’s sake):</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public async Task UploadImageAsync(byte[] imageData, string fileName, object objectParameter, CancellationToken cancellationToken, params string[] pathComponents)
  {
      ...
  
      using (MemoryStream stream = new MemoryStream(imageData))
      {
          using (HttpContent fileStreamContent = new StreamContent(stream))
          {
              fileStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "file", FileName = fileName };
              fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
              using (MultipartFormDataContent formData = new MultipartFormDataContent())
              {
                  formData.Add(fileStreamContent);
                  string apiPath = String.Join(Globals.PathSeparator, pathComponents);
                  if (objectParameter != null)
                  {
                      apiPath = apiPath + "?" + objectParameter.ToQueryString();
                  }
                  await this.SendPostAsync(apiPath, formData, cancellationToken).ConfigureAwait(false))
              }
          }
      }
  } 
  
  private Task&lt;HttpResponseMessage&gt; SendPostAsync(string path, HttpContent content, CancellationToken cancellationToken)
  {
      return this.SendRequestAsync(HttpAction.Post, path, content, cancellationToken);
  } 
  
  private async Task&lt;HttpResponseMessage&gt; SendRequestAsync(HttpAction action, string path, HttpContent content, CancellationToken cancellationToken)
  {
      ...
      switch (action)
      {
          ...
          case HttpAction.Post:
          response = await this._client.PostAsync(path, content, cancellationToken).ConfigureAwait(false);
          break;
          ...
      }
  ...
  return response;
  }
  </code></pre>
  
  <p><strong>Web service controller code (also abbreviated):</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>[Route("api/[controller]")]
  public class DocumentsController : ControllerBase
  { 
      private readonly IDocumentsRepository _documents; 
      ...
  
      public DocumentsController(IDocumentsRepository documents, ...)
      {
          this._documents = documents;
          ...
      }
      ...
  
      [HttpPost(nameof(SaveImage))]
      [Authorize(JwtBearerDefaults.AuthenticationScheme)]
      public IActionResult SaveImage(IFormFile file, [FromQuery]ServiceReportIdentification documentInfo)
      {
          this._documents.SaveImage(file, documentInfo);
          return this.CreatedAtAction(nameof(SaveImage), file);
      }
      ...
  }
  </code></pre>
  
  <h2>Request size limit</h2>
  
  <p>The default IIS request size limit should be at 4 MB and most images I tried to upload were less than 1 MB, so this shouldn’t be an issue. However, I still tried manually setting all size limits I could find to see if I can override that hidden 64 KB size limit. No dice.</p>
  
  <p>I even copied the <code>httpCompression</code> tag from the .vs\config\applicationhost.config file of the example solution, which was the only real difference between that one and the one form my actual web service solution.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;httpRuntime maxRequestLength="10240" /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;security&gt;
        &lt;requestFiltering&gt;
          &lt;requestLimits maxAllowedContentLength="10485760" /&gt;
        &lt;/requestFiltering&gt;
      &lt;/security&gt;
      &lt;handlers&gt;
        &lt;add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /&gt;
      &lt;/handlers&gt;
      &lt;aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00" /&gt;
      &lt;httpCompression&gt;
          &lt;dynamicCompression&gt;
              &lt;add mimeType="text/event-stream" enabled="false" /&gt;
          &lt;/dynamicCompression&gt;
      &lt;/httpCompression&gt;
    &lt;/system.webServer&gt;
      &lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;wsHttpBinding&gt;
                  &lt;binding name="ServicePlusApi" maxReceivedMessageSize="10485760" /&gt;
              &lt;/wsHttpBinding&gt;
          &lt;/bindings&gt;
      &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <h2>Some background</h2>
  
  <p>When I created the web service solution, Asp.Net Core hasn’t reached version 2.0 yet, which is why there’s still a web.config file present in the Web API project. Since 2.0 (or at least that’s my assumption), the web.config file does no longer exist. Instead, the default configuration is stowed away inside the .vs\config\applicationhost.config file.</p>
  
  <p>For that reason, I created a fresh solution and Web API project, added in the code from my web service as well as additional projects needed for referencing, essentially recreating the whole solution with the same codebase but latest project structure/definition.</p>
  
  <p>Still, this didn’t fly.</p>
  
  <hr>
  
  <p>I have no clue what is causing that weird 64 KB request size limit, which doesn’t even return an error, nor is there any record of the request in the IIS logs. It just swallows the request, never providing any sort of response (besides a client-side timeout).</p>
  
  <p>Does anyone have any idea what could be the cause of this? I’ve scoured the internet for quite a while now and besides those manual size limit overrides, which don’t work in this case, I found nothing that helps.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The above described behavior happens locally on a dev machine in debug mode using Visual Studio's IIS Express. The same applies when the web service is published in release mode and hosted by a standard IIS on a different machine.</p>
  
  <h2>UPDATE</h2>
  
  <p>Upon searching the IIS trace logs for more clues, I found the following event:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"&gt;
   &lt;System&gt;
    ...
   &lt;/System&gt;
   &lt;EventData&gt;
    &lt;Data Name="ContextId"&gt;{8000006B-0006-FF00-B63F-84710C7967BB}&lt;/Data&gt;
    &lt;Data Name="ModuleName"&gt;AspNetCoreModule&lt;/Data&gt;
    &lt;Data Name="Notification"&gt;128&lt;/Data&gt;
    &lt;Data Name="HttpStatus"&gt;400&lt;/Data&gt;
    &lt;Data Name="HttpReason"&gt;Bad Request&lt;/Data&gt;
    &lt;Data Name="HttpSubStatus"&gt;0&lt;/Data&gt;
    &lt;Data Name="ErrorCode"&gt;2147952454&lt;/Data&gt;
    &lt;Data Name="ConfigExceptionInfo"&gt;&lt;/Data&gt;
   &lt;/EventData&gt;
   &lt;RenderingInfo Culture="de-CH"&gt;
    &lt;Opcode&gt;MODULE_SET_RESPONSE_ERROR_STATUS&lt;/Opcode&gt;
    &lt;Keywords&gt;
     &lt;Keyword&gt;RequestNotifications&lt;/Keyword&gt;
    &lt;/Keywords&gt;
    &lt;freb:Description Data="Notification"&gt;EXECUTE_REQUEST_HANDLER&lt;/freb:Description&gt;
    &lt;freb:Description Data="ErrorCode"&gt;Eine vorhandene Verbindung wurde vom Remotehost geschlossen.
   (0x80072746)&lt;/freb:Description&gt;
   &lt;/RenderingInfo&gt;
   ...
  &lt;/Event&gt;
  </code></pre>
  
  <p>The error description is in German and says: "An existing connection has been closed by the remote host."</p>
  
  <p>A little further down, I found some response data which contains buffer data for the error page (which never shows due to the nature of the web service), containing the following error text: "The request could not be understood by the server due to malformed syntax."</p>
  
  <p>Strangely, there's nor a request, nor a response entry within the normal IIS log. Just in the trace log.</p>