【问题标题】:System.OutOfMemoryException: 'Exception_WasThrown' [duplicate]System.OutOfMemoryException:'Exception_WasThrown' [重复]
【发布时间】:2018-03-29 15:05:47
【问题描述】:

按照我的代码:

[HttpGet]
public ActionResult StreamVideo(int type1, int type2)
{
    using (var ctx = new Entities())
    {
        var result = ctx.Table.Where(x => x.Type1 == type1 && x.Type2 == type2).FirstOrDefault();

        byte[] video_byte = result.Video;

        return new RangeFileContentResult(video_byte, "video/x-msvideo", "NameFile.mp4", DateTime.Now);
    }
}

我有一个“模态引导程序”,其中包含视频内容。关闭模态并再次打开时,会出现问题:

System.OutOfMemoryException: 'Exception_WasThrown'

在线出现问题:

var result = ctx.Table.Where(x => x.Type1 == type1 && x.Type2 == type2).FirstOrDefault();

有什么办法吗?

【问题讨论】:

  • 返回什么?列中有多少字节?我猜Video 是一个 varbinary / image 类型字段,而您将 GB 存储在该字段中。
  • result.Video 有多大?也许你正在破坏你的Large Object Heap
  • " 我猜 Video 是一个 varbinary" 完全正确
  • 你试过.AsNoTracking() 吗? ctx.Table.AsNoTracking().Where(....
  • (关于你的更新) 这是完全不同的代码。您原来的帖子是关于检索。编辑后的问题是关于文件的发布(上传)。如果您还有其他问题,您应该将其作为一个新问题提出,不要更改您现有的问题并添加一个(大部分)不相关的问题。

标签: c# asp.net lib.web.mvc


【解决方案1】:

在检索大量varbinary 数据时,您需要注意不要使大对象堆负担过重。考虑改为以流的形式检索数据。 EntityCommandSqlCommand 都可以检索读者,你可以从他们那里得到一个流。

SqlClient Streaming

using (connection)
{
    SqlCommand command = new SqlCommand(
      $"SELECT Video FROM Table where Type1={type1} and Type2={type2};",
      connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    reader.Read();
    var stream = reader.GetStream(0);

   ... Use the stream here...
}

【讨论】:

  • 我用github.com/tpeczek/Lib.Web.Mvc播放视频。线路:return new RangeFileContentResult(video_byte, "video/x-msvideo", "NameFile.mp4", DateTime.Now);不起作用?
  • 试试public RangeFileStreamResult(Stream fileStream, string contentType, string fileName, DateTime modificationDate)
  • Stream fileStream 是什么?你能帮我吗?
  • 如果您在 SqlCommandEntityCommand 上使用 ExecuteReader 命令,就像我在上面的示例中所做的那样,那么您可以使用 Stream 访问 varbinary 987654336@以上电话。在调用RangeFIleStreamResult() 时使用它而不是byte[] GetStream() 不必将整个视频作为单个对象进行检索,但可以让您像访问文件一样访问数据。 msdn.microsoft.com/en-us/library/…
  • 流让您可以操作大量数据,而无需一次将所有数据保存在内存中。 msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
相关资源
最近更新 更多