System.OutOfMemoryException:' Exception_WasThrown'

时间:2017-10-17 20:06:15

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

按照我的代码:

[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'

enter image description here

问题在线发生:

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

任何解决方案?

1 个答案:

答案 0 :(得分: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...
}