1

Follow my code:

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

I have a "modal bootstrap" where it has video content.When closing modal and opening again, it gives problem:

System.OutOfMemoryException: 'Exception_WasThrown'

enter image description here

Problem occurs on line:

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

Any solution ?

Matheus Miranda
  • 1,755
  • 2
  • 21
  • 36
  • What is being returned? How many bytes in the column(s)? I am going to guess that `Video` is a varbinary / image type field and you are storing GBs in that field. – Igor Oct 17 '17 at 20:08
  • 1
    How big is `result.Video`? Maybe you are clobbering your [Large Object Heap](https://www.red-gate.com/simple-talk/dotnet/.net-framework/the-dangers-of-the-large-object-heap/). – bigtlb Oct 17 '17 at 20:11
  • " I am going to guess that Video is a varbinary " Exactly – Matheus Miranda Oct 17 '17 at 20:12
  • 1
    Have you tried `.AsNoTracking()` ? `ctx.Table.AsNoTracking().Where(....` – Igor Oct 17 '17 at 20:12
  • @bigtlb, Video has size of 98MB. – Matheus Miranda Oct 17 '17 at 20:12
  • I'll try with ".AsNoTracking ()". Wait. – Matheus Miranda Oct 17 '17 at 20:14
  • Now gives the same conversion problem, I'll edit post. – Matheus Miranda Oct 17 '17 at 20:28
  • 1
    (*about your update*) This is completely different code. Your original post was about **retrieval**. The edited question is about **posting** (upload) of a file. If you have an additional question you should ask it as a new question, do not change your existing question and add an (mostly) unrelated problem to it. – Igor Oct 17 '17 at 20:34
  • Guys, can you guys help me? https://stackoverflow.com/questions/46811925/element-video-does-not-play-using-rangefilestreamresult Based on the response of friend @bigtlb – Matheus Miranda Oct 18 '17 at 14:19

1 Answers1

1

When retrieving large amounts of varbinary data you need to be careful that you don't overtax the Large Object Heap. Consider retrieving the data as a stream instead. EntityCommand and SqlCommand can both retrieve readers, and you can get a stream from them.

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...
}
bigtlb
  • 1,512
  • 10
  • 16
  • I use https://github.com/tpeczek/Lib.Web.Mvc to play video.The line: `return new RangeFileContentResult(video_byte, "video/x-msvideo", "NameFile.mp4", DateTime.Now);` does not work ? – Matheus Miranda Oct 17 '17 at 20:34
  • 1
    Try `public RangeFileStreamResult(Stream fileStream, string contentType, string fileName, DateTime modificationDate)` – bigtlb Oct 17 '17 at 20:37
  • Whats is `Stream fileStream` ? Can you help me ? – Matheus Miranda Oct 17 '17 at 20:45
  • 1
    If you use an `ExecuteReader` command on either a `SqlCommand` or an `EntityCommand`, like I did in the sample above, then you can access the varbinary as a `Stream` using the `GetStream(0)` call above. Use that instead of a `byte[]` when calling `RangeFIleStreamResult()` `GetStream()` doesn't have to retrieve the entire video as a single object, but will then let you access the data like you would a file. https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstream(v=vs.110).aspx – bigtlb Oct 17 '17 at 21:33
  • 1
    Streams let you manipulate large amounts of data without having to hold it all in memory at one time. https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx – bigtlb Oct 17 '17 at 21:34
  • bigtlb, Please see it: https://stackoverflow.com/questions/46811925/element-video-does-not-play-using-rangefilestreamresult – Matheus Miranda Oct 18 '17 at 14:15