0

I'm creating a website with ASP.NET MVC5, hosted on Azure.
My users may upload a video, and I'd like to create a progress bar or something indicating how much % of the upload / transcoding has been done.

I'm following this Microsoft tutorial and the videos are correctly uploaded.
However, they show these lines of code once the job is sumbitted to Azure :

job = job.StartExecutionProgressTask(
  j =>
  {
      Console.WriteLine("Job state: {0}", j.State);
      Console.WriteLine("Job progress: {0:0.##}%", j.GetOverallProgress());
  },
  CancellationToken.None).Result;

I'm trying to adapt it to show it on my webpage.

Question

However, I'm unable to "send" j.GetOverallProgress() to my view.
Can anyone explain how to do this ?


What I did so far

Install Microsoft.AspNet.SignalR package (v 2.4.1)
(first time using SignalR)

Add these lines in my view :

<script src="~/Scripts/signalR/jquery.signalR-2.4.1.js"></script>
<script src="~/signalr/hubs"></script>

<script type="text/javascript">
    $(document).ready(function () {
        var hub = $.connection.videoService;

        $("#btnCreate").on("click", function () {
            hub.client.displayProgress = function (data) {
                console.log(data); //Nothing is logged
            };
        })
    });
</script>

And these lines on my back-end : (VideoService.cs in \Services folder)

public class VideoService : Hub
{
    public EncodeMyVideo(...)
    {
        /* [...] */
        
        job = job.StartExecutionProgressTask(j => { 
            displayProgress(job); 
        }, CancellationToken.None).Result;
    }

    public double displayProgress(IJob job)
    {
        return job.GetOverallProgress();
    }
}

In debug mode, it goes to displayProgress method but never send anything to the view.

As a aside note, I also tried ith this :

public void displayProgress(IJob job)
{
    Clients.Caller.displayProgress(job.GetOverallProgress());
}

But I get this error :

Using a Hub instance not created by the HubPipeline is unsupported.

I'm very new with SignalR, and despite this SO answer, I don't really understand what the problem is.

What should I do to send job.GetOverallProgress(); into my view ?

Community
  • 1
  • 1
AlexB
  • 7,302
  • 12
  • 56
  • 74

1 Answers1

0

displayProgress method runs in another thread so it does not share the same Context with the EncodeMyVideo method. So you need to use GlobalHost.ConnectionManager.GetHubContext<> to get your hub context and your client connections.

string callerId = Context.ConnectionId;
job = job.StartExecutionProgressTask(j => { 
            displayProgress(job, callerId); 
        }, CancellationToken.None).Result;

public void displayProgress(IJob job, string clientId)
{
  //Access to your hub context outside the request context
   GlobalHost.ConnectionManager.GetHubContext<VideoService>().Clients.Client(clientId).displayProgress(job.GetOverallProgress());
}  

For reporting progress you may also check this Reporting progress from hub method invocations

NthDeveloper
  • 969
  • 8
  • 16