1

I want to convert video file using FFMPEG at server side which is long running process. I decided to send AJAX request to server so conversion process should run parallel. But my controller is serving single request at a time due to this its taking long time to complete the task. I have investigated issue and I came to know that SessionState make's controller execution serialized. Here explained same issue in more details. I want to disable SessionState behaviour in MVC 2.

[SessionState(SessionStateBehavior.Disabled)] 

This is added in MVC 3+. How to achieve same with MVC 2

I have simple Controller Action where I am calling CovertFile method wich convert the file using FFMPEG, Below is reference code

    [HttpPost]
    public ActionResult PostRecordedAudioVideoTemp()
    { 
         string outFile = ConvertFile(audioFileName, videoFileName, output, logFileConversion);
         return Json(new { fileName = Request.Form[0], uploaded = true, type = fileType });
    }

UPDATE :

To confirm about controller is really serialized, I run flollowing test :

    AudioVideoController
    {    
      public ActionResult Ajax()
        {
            var startTime = DateTime.Now;
            Thread.Sleep(_random.Next(5000, 10000));
            return Json(new
            {
                conttroller = "AudioVideo",
                startTime = startTime.ToString("HH:mm:ss fff"),
                endTime = DateTime.Now.ToString("HH:mm:ss fff")
            }, JsonRequestBehavior.AllowGet);
        }
    }

In View :

$(function () {
    for (var i = 0; i < 6; i++) {
        $.getJSON('/AudioVideo/ajax', function (result) {
            console.log(
                result.startTime + ' | ' + result.endTime + '|'+ result.conttroller
            )
        });
    }
});

Result :

14:14:44 562 | 14:14:51 978|AudioVideo
14:14:52 313 | 14:14:57 821|AudioVideo
14:14:57 897 | 14:15:04 580|AudioVideo
14:15:04 995 | 14:15:13 486|AudioVideo
14:15:13 607 | 14:15:21 692|AudioVideo
14:15:21 718 | 14:15:28 459|AudioVideo

Chrome Network console :

enter image description here

From the result its clear that ajax call to controller are blocking. Request can not initiate unless previous ajax request not complete so controller is serialised and serving one request at a time.


UPDATE 2:
I thinks to create two separate controller with same method and send ajax request to separate controller simultaneously to check if they executed parallel

 AudioVideo1Controller 
 public ActionResult Ajax()
    {
        HttpContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);

        var startTime = DateTime.Now;
        Thread.Sleep(_random.Next(5000, 10000));
        return Json(new
        {
            conttroller = "AudioVideo1",
            startTime = startTime.ToString("HH:mm:ss fff"),
            endTime = DateTime.Now.ToString("HH:mm:ss fff")
        }, JsonRequestBehavior.AllowGet);
    }

JavaScript :

 $(function () {
        for (var i = 0; i < 6; i++) {
            $.getJSON('/AudioVideo/ajax', function (result) {
                console.log(
                    result.startTime + ' | ' + result.endTime + '|'+ result.conttroller
                )
            });
        }
    });
    
$(function () {
        for (var i = 0; i < 6; i++) {
            $.getJSON('/AudioVideo1/ajax', function (result) {
                console.log(
                    result.startTime + ' | ' + result.endTime + '|'+ result.conttroller
                )
            });
        }
    });

Result :

 14:33:37 355 | 14:33:46 642|AudioVideo
 14:33:46 649 | 14:33:53 782|AudioVideo1
 14:33:53 790 | 14:34:00 574|AudioVideo1
 14:34:00 580 | 14:34:06 147|AudioVideo1
 14:34:06 151 | 14:34:12 124|AudioVideo1
 14:34:12 127 | 14:34:20 766|AudioVideo1
 14:34:20 770 | 14:34:29 589|AudioVideo1
 14:34:29 792 | 14:34:39 112|AudioVideo
 14:34:39 419 | 14:34:44 942|AudioVideo
 14:34:45 003 | 14:34:54 171|AudioVideo
 14:34:54 643 | 14:35:03 013|AudioVideo
 14:35:03 255 | 14:35:12 062|AudioVideo

Result are shocking. Even two different controller can not serve request simultaneously.

I am using .NET MVC 2 How to make controller asynchronous ?
Does anyone has workaround for this.

Community
  • 1
  • 1
Dipak Telangre
  • 1,792
  • 4
  • 19
  • 46

0 Answers0