title: Not able to return CSV file in MVC action. Not Cannot access a closed Streamable
body: <p>On a ASP.NET MVC action I am trying to return a CSV file with users info:</p>
  
  <pre><code>  IList&lt;UserModel&gt; users = _repository.GetUsers();
  
    MemoryStream stream = new MemoryStream;
      using (StreamWriter writer = new StreamWriter(stream)) {
        using (CsvWriter csv = new CsvWriter(writer)) {
          csv.Configuration.With(x =&gt; {
            x.AutoMap&lt;UserModel&gt;();
            x.RegisterClassMap&lt;UserModelCsvMapper&gt;();
          });            
          csv.WriteRecords(reply.Users);
          writer.Flush();
          return File(stream, "text/csv", "Users.csv");
        }
      }
  </code></pre>
  
  <p>And I have the following mapper:</p>
  
  <pre><code>public class UserModelCsvMapper : CsvClassMap&lt;UserModel&gt; {
    public override void CreateMap() {
      Map(x =&gt; x.Name).Name("Name");
      Map(x =&gt; x.Email).Name("Email");
      Map(x =&gt; x.Linked).Name("Linked");
    } // CreateMap
  } // UserModelCsvMapper
  </code></pre>
  
  <p>My CSV writer seems empty and when I return the Stream I get the error:</p>
  
  <pre><code>((System.IO.Stream)(stream)).WriteTimeout' threw an exception of type 'System.InvalidOperationException'
  </code></pre>
  
  <p>And I get the error page:</p>
  
  <pre><code>Cannot access a closed Stream.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  
  Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.
  </code></pre>
  
  <p><strong>UPDATE: Adding Stack Trace</strong></p>
  
  <p>Cannot access a closed Stream.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  
  <p>Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.</p>
  
  <p>Source Error:</p>
  
  <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  
  <p>Stack Trace:</p>
  
  <p>[ObjectDisposedException: Cannot access a closed Stream.]
     System.IO._<em>Error.StreamIsClosed() +57
     System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count) +10909062
     System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) +157
     System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +296
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList<code>1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList</code>1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList<code>1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList</code>1 filters, ActionResult actionResult) +106
     System.Web.Mvc.Async.&lt;>c</em>_DisplayClass2b.b_<em>1c() +321
     System.Web.Mvc.Async.&lt;>c</em>_DisplayClass21.b_<em>1e(IAsyncResult asyncResult) +185
     System.Web.Mvc.Async.WrappedAsyncResult<code>1.CallEndDelegate(IAsyncResult asyncResult) +42
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +133
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
     System.Web.Mvc.Controller.b</em>_1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +70
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
     System.Web.Mvc.Controller.b_<em>15(IAsyncResult asyncResult, Controller controller) +39
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +62
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.MvcHandler.b</em>_5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +70
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155</p>
  
  <p>Thank You,
  Miguel</p>