title: C# MemoryStream - Timeouts are not supported on this stream
body: <p>I'm trying to create a csv file dynamially and output the stream to the browser.</p>
  
  <p>Here is my api end point:</p>
  
  <pre><code>    [System.Web.Http.HttpGet]
      [System.Web.Http.Route("export-to-csv")]
      public FileStreamResult ExportDeposits([FromUri(Name = "")]DepositSearchParamsVM depositSearchParamsVM)
          {
              if (depositSearchParamsVM == null)
              {
                  depositSearchParamsVM = new DepositSearchParamsVM();
              }
              var records = _DepositsService.SearchDeposits(depositSearchParamsVM);
              var result = _DepositsService.WriteCsvToMemory(records);
              var memoryStream = new MemoryStream(result);
  
              return new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" };
      }
  </code></pre>
  
  <p>Here is my service method:</p>
  
  <pre><code>public byte[] WriteCsvToMemory(IEnumerable&lt;DepositSummaryVM&gt; records)
          {
              using (var stream = new MemoryStream())
              using (var reader = new StreamReader(stream))
              using (var writer = new StreamWriter(stream))
              using (var csv = new CsvWriter(writer))
              {
                  csv.WriteRecords(records);
                  writer.Flush();
                  stream.Position = 0;
                  var text = reader.ReadToEnd();
  
                  return stream.ToArray();
  </code></pre>
  
  <p>}</p>
  
  <pre><code>    }
  </code></pre>
  
  <p>Here is the error message:</p>
  
  <blockquote>
    <p>{   "message": "An error has occurred.",   "exceptionMessage": "Error
    getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.",<br>
    "exceptionType": "Newtonsoft.Json.JsonSerializationException",<br>
    "stackTrace": "   at
    Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object
    target)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter
    writer, Object value, JsonContainerContract contract, JsonProperty
    member, JsonProperty property, JsonContract&amp; memberContract, Object&amp;
    memberValue)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter
    writer, Object value, JsonObjectContract contract, JsonProperty
    member, JsonContainerContract collectionContract, JsonProperty
    containerProperty)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter
    writer, Object value, JsonContract valueContract, JsonProperty member,
    JsonContainerContract containerContract, JsonProperty
    containerProperty)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter
    writer, Object value, JsonObjectContract contract, JsonProperty
    member, JsonContainerContract collectionContract, JsonProperty
    containerProperty)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter
    writer, Object value, JsonContract valueContract, JsonProperty member,
    JsonContainerContract containerContract, JsonProperty
    containerProperty)\r\n   at
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter
    jsonWriter, Object value, Type objectType)\r\n   at
    Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter
    jsonWriter, Object value, Type objectType)\r\n   at
    System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type
    type, Object value, Stream writeStream, Encoding
    effectiveEncoding)\r\n   at
    System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type
    type, Object value, Stream writeStream, Encoding
    effectiveEncoding)\r\n   at
    System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type
    type, Object value, Stream writeStream, HttpContent content)\r\n   at
    System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type
    type, Object value, Stream writeStream, HttpContent content,
    TransportContext transportContext, CancellationToken
    cancellationToken)\r\n--- End of stack trace from previous location
    where exception was thrown ---\r\n   at
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
    task)\r\n   at
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
    task)\r\n   at
    System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()",
    "innerException": {
        "message": "An error has occurred.",
        "exceptionMessage": "Timeouts are not supported on this stream.",
        "exceptionType": "System.InvalidOperationException",
        "stackTrace": "   at System.IO.Stream.get_ReadTimeout()\r\n   at GetReadTimeout(Object )\r\n   at
    Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object
    target)"   } }</p>
  </blockquote>