title: Random "error reading MIME multipart body" in Web API
body: <p>Occasionally, an upload of a gzipped file from a phone app to the web service fails with the following error:</p>
  
  <pre><code>Error reading MIME multipart body part.
  
  at System.Net.Http.HttpContentMultipartExtensions.&lt;MultipartReadAsync&gt;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 System.Net.Http.HttpContentMultipartExtensions.&lt;ReadAsMultipartAsync&gt;d__0`1.MoveNext()
  </code></pre>
  
  <p>The endpoint itself is pretty basic:</p>
  
  <pre><code>    [System.Web.Http.HttpPost]
      public async Task&lt;HttpResponseMessage&gt; TechAppUploadPhoto()
      {
          if (!Request.Content.IsMimeMultipartContent())
          {
              return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "The request isn't valid!");
          }
  
          try
          {
              var provider = new MultipartMemoryStreamProvider();
              await Request.Content.ReadAsMultipartAsync(provider);
              foreach (StreamContent file in provider.Contents)
              {
                  Stream dataStream = await file.ReadAsStreamAsync();
                  String fileName = file.Headers.ContentDisposition.FileName;
                  fileName = [unique name];
  
                  String filePath = Path.Combine(ConfigurationManager.AppSettings["PhotoUploadLocation"], fileName);
  
                  using (var fileStream = File.Create(filePath))
                  {
                      dataStream.Seek(0, SeekOrigin.Begin);
                      dataStream.CopyTo(fileStream);
                      fileStream.Close();
                      dataStream.Close();
                  }
  
                  // Enable overwriting with ZipArchive
                  using (ZipArchive archive = ZipFile.OpenRead(filePath))
                  {
                      foreach (ZipArchiveEntry entry in archive.Entries)
                      {
                          entry.ExtractToFile(Path.Combine(ConfigurationManager.AppSettings["PhotoUploadLocation"], entry.FullName), true);
                      }
                  } 
                  File.Delete(filePath);
              }
  
              return Request.CreateResponse(HttpStatusCode.Accepted);
          }
          catch (Exception e)
          {
              return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "PostCatchErr: " + e.Message + e.StackTrace);
          }
      }
  </code></pre>
  
  <p>This error appears to be pretty random and hard to recreate. Unfortunately, I didn't write either end and don't have much experience here, but it doesn't appear to be related to the size of the upload either - it's a large limit in the web config and larger files can be uploaded than those that error out. Is there something I'm missing that could cause this issue? It only appears to read the body information once, which is the other possible cause I've found. Any thoughts?</p>