Need an ASP.NET MVC long running process with user feedback

前端 未结 3 1320
悲&欢浪女
悲&欢浪女 2020-12-02 05:19

I\'ve been trying to create a controller in my project for delivering what could turn out to be quite complex reports. As a result they can take a relatively long time and a

相关标签:
3条回答
  • 2020-12-02 05:45

    4.5 years after this question has been answered, and we have a library that can make this task much easier: SignalR. No need to use shared state (which is bad because it can lead to unexpected results), just use the HubContext class to connect to a Hub that sends messages to the client.

    First, we set up a SignalR connection like usual (see e.g. here), except that we don't need any server-side method on our Hub. Then we make an AJAX call to our Endpoint/Controller/whatever and pass the connection ID, which we get as usual:var connectionId = $.connection.hub.id;. On the server side of things, you can start your process on a different thread and retutn 200 OK to the client. The process will know the connectionId so that it can send messages back to the client, like this:

    GlobalHost.ConnectionManager.GetHubContext<LogHub>()
                                  .Clients.Client(connectionId)
                                  .log(message);
    

    Here, log is a client-side method that you want to call from the server, hence it should be defined like you usually do with SignalR:

    $.connection.logHub.client.log = function(message){...};
    

    More details in my blog post here

    0 讨论(0)
  • 2020-12-02 05:46

    Here's a sample I wrote that you could try:

    Controller:

    public class HomeController : AsyncController
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public void SomeTaskAsync(int id)
        {
            AsyncManager.OutstandingOperations.Increment();
            Task.Factory.StartNew(taskId =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(200);
                    HttpContext.Application["task" + taskId] = i;
                }
                var result = "result";
                AsyncManager.OutstandingOperations.Decrement();
                AsyncManager.Parameters["result"] = result;
                return result;
            }, id);
        }
    
        public ActionResult SomeTaskCompleted(string result)
        {
            return Content(result, "text/plain");
        }
    
        public ActionResult SomeTaskProgress(int id)
        {
            return Json(new
            {
                Progress = HttpContext.Application["task" + id]
            }, JsonRequestBehavior.AllowGet);
        }
    }
    

    Index() View:

    <script type="text/javascript">
    $(function () {
        var taskId = 543;
        $.get('/home/sometask', { id: taskId }, function (result) {
            window.clearInterval(intervalId);
            $('#result').html(result);
        });
    
        var intervalId = window.setInterval(function () {
            $.getJSON('/home/sometaskprogress', { id: taskId }, function (json) {
                $('#progress').html(json.Progress + '%');
            });
        }, 5000);
    });
    </script>
    
    <div id="progress"></div>
    <div id="result"></div>
    

    The idea is to start an asynchronous operation that will report the progress using HttpContext.Application meaning that each task must have an unique id. Then on the client side we start the task and then send multiple AJAX requests (every 5s) to update the progress. You may tweak the parameters to adjust to your scenario. Further improvement would be to add exception handling.

    0 讨论(0)
  • 2020-12-02 05:57

    If you have an access to your web server machine as a choice you can create windows service or simple console application to perform long running work. Your web app should add a record to db to indicate that operation should start, and your windows service which periodicaly checks for new records in db should start performing task and report progress to db. Your web app can use ajax request to check for progress and show to users.

    I used this method to implement excel reports for asp.net mvc application. This report was created by windows service which runs on machine and constantly checks for new records in reports table and when it finds a new record it starts to create a report and indicate a progress by updating record field. Asp.net mvc application just has been adding new report record and tracking progress in database untill it finished and then provided a link to ready file to be downloaded.

    0 讨论(0)
提交回复
热议问题