title: Why does ASP.NET MVC Controller FileStreamResult error on closed stream?
body: <p>I am trying to return a csv file from an MVC Controller derived from a List.  I came up with the following code for controller based on combining what I read from docs and SO questions.  I must be missing something because I am getting yellow screen of death with "Cannot access a closed stream"...</p>
  
  <pre><code>public class ConsumersFileController : Controller
  {
      private readonly TDCContext _db = new TDCContext();
  
  
      public ActionResult Index()
      {
          IEnumerable&lt;Consumer&gt; list = _db.Consumers.ToList();
  
          //put List in memory stream object
          MemoryStream memoryStream = new MemoryStream();
          using (memoryStream)
          {
              //return memory stream as file stream result:
              using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream, System.Text.Encoding.UTF8, true))
              {
                  foreach (var item in list)
                  {
                      var itemBytes = item.Serialize();
                      binaryWriter.Write(itemBytes.Length);
                      binaryWriter.Write(itemBytes);
  
                  }
  
                  FileStreamResult fileStream =
                      new FileStreamResult(memoryStream, "application/txt") {FileDownloadName = "zips.csv"};
  
                  return fileStream;
              }
          }
      }
  
      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              _db.Dispose();
          }
          base.Dispose(disposing);
      }
  
  }
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>[ObjectDisposedException: Cannot access a closed Stream.]
     System.IO.__Error.StreamIsClosed() +59
     System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count) +14731549
     System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) +93
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +88
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +831
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +81
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +185
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
     System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
     System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +607
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +134
  </code></pre>
  
  <p>How can the stream get closed if I am returning the result inside the <code>using</code> block of the stream?</p>