C#MemoryStream - 此流不支持超时

时间:2018-01-01 17:18:32

标签: c# csv memorystream csvhelper

我尝试动态创建csv文件并将流输出到浏览器。

这是我的api终点:

    [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" };
    }

这是我的服务方法:

public byte[] WriteCsvToMemory(IEnumerable<DepositSummaryVM> 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();

}

    }

以下是错误消息:

  

{&#34; message&#34;:&#34;发生错误。&#34;,&#34; exceptionMessage&#34;:&#34;错误   从“读取时间”中获取价值&#39; on&#39; System.IO.MemoryStream&#39;。&#34;,
  &#34; exceptionType&#34;:&#34; Newtonsoft.Json.JsonSerializationException&#34;,
  &#34; stackTrace&#34;:&#34;在   Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象   目标)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter   writer,Object value,JsonContainerContract contract,JsonProperty   成员,JsonProperty财产,JsonContract&amp; memberContract,Object&amp;   memberValue)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter   writer,Object value,JsonObjectContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter   writer,Object value,JsonObjectContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   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,Object value,Stream writeStream,Encoding   有效编码)\ r \ n at   System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型   type,Object value,Stream writeStream,Encoding   有效编码)\ r \ n at   System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型   type,Object value,Stream writeStream,HttpContent content)\ r \ n at   System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型   type,Object value,Stream writeStream,HttpContent内容,   TransportContext transportContext,CancellationToken   cancellationToken)\ r \ n ---从前一个位置开始的堆栈跟踪结束   抛出异常的地方--- \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()&#34 ;,   &#34; innerException&#34;:{       &#34; message&#34;:&#34;发生了错误。&#34;,       &#34; exceptionMessage&#34;:&#34;此流不支持超时。&#34;,       &#34; exceptionType&#34;:&#34; System.InvalidOperationException&#34;,       &#34; stackTrace&#34;:&#34;在System.IO.Stream.get_ReadTimeout()\ r \ n在GetReadTimeout(Object)\ r \ n at   Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象   目标)&#34; }}

1 个答案:

答案 0 :(得分:0)

Api正试图将<%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Connection"%> 序列化为json。那么,您是否会尝试更改MemoryStream;

ContentType