title: c# - Unexpected end of Stream, the content may have already been read by another component
body: <p>I've been trying to get this to work, but no luck.</p>
  
  <p>I have middleware class where used to verify uploaded files.</p>
  
  <p>After middleware debug goes to the Controller class, but not to the action. Context Request ending after controller's constructor.</p>
  
  <p>Middleware class:</p>
  
  <pre><code> public class UploadMiddleware
  {
  
      public UploadMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
          [DisableFormValueModelBinding]
          [ValidateAntiForgeryToken]
      public async Task Invoke(HttpContext context)
      {
          var authToken = context.Request.Headers["id"].ToString();
          var path = context.Request.Path.ToString();
          var streamBody = context.Request.Body;
  
              if (authToken != String.Empty)
              {
                  if (!IsMultipartContentType(context.Request.ContentType))
                  {
                      await context.Response.WriteAsync("Unexpected error.");  
                      await _next(context);              
                      return;
                  }
  
                  var boundary = GetBoundary(context.Request.ContentType); 
                  var reader = new MultipartReader(boundary, context.Request.Body);
                  // reader.HeadersLengthLimit = int.MaxValue;
                  // reader.BodyLengthLimit = long.MaxValue;
  
                  var section = await reader.ReadNextSectionAsync();
                  var buffer = new byte[256];
                  while (section != null)
                  {
                      var fileName = GetFileName(section.ContentDisposition);
                      var bytesRead = 0;
  
                      using (var stream = new FileStream(fileName,FileMode.Append))
                      {                                         
                          do{
  
                              bytesRead = await section.Body.ReadAsync(buffer, 0, buffer.Length);
                              stream.Write(buffer, 0, bytesRead);       
                               buffer = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(buffer));
                         } while (bytesRead &lt; 0);                   
                      }
  
  
                      if (!verifyUpcomingFiles(buffer))
                      {
                          await context.Response.WriteAsync("Undefined file type detected.");
                          return;
                      }
                       else
                       {
                        context.Request.Headers.Add("isVerified","verified");                                                    
                      }
  
  
                       section = await reader.ReadNextSectionAsync();      
                  }
  
  
              }
              else
              {
                  await context.Response.WriteAsync("You are not allowed to complete this process.");
                  return;
  
              }
              await _next(context);
  
  
      }
  </code></pre>
  
  <p>I am stuck in this problem. I really need someone point me in a direction, that would be greatly appreciated. </p>