Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an async controller and updating progress

I am currently looking into making use of the async/await functionality to:

  1. submit a file.
  2. This file for example could contain one or more entries. (eg. Could be an XML file with only one element of , or it could have multiple nodes listed per file)
  3. I need to process each Call individually, add it to a list of completed items, and give back progress as once an individual task completion. (Completed: 0/2 ----> Completed 1/2...)
  4. This needs to be send back to the view so whoever is using this feature can see the individual Call has been imported.

To achieve this I will be using Json to update the page/progressbar asynchronously.

The problem I'm foreseeing is this:

When I call the submit to go to a controller, I will need a response for each individual Call. I do not know how many Calls are in the file until I have submitted the file and completed some processing already (to determine each Call node). Is there a clean approach to getting a response for each completed item I add to the list?

like image 316
Luke G Avatar asked Dec 21 '22 15:12

Luke G


2 Answers

Unfortunately, async and await cannot do this. As I explain on my blog, async does not change the HTTP protocol; you get one response per request, that's it.

So, when the file is submitted, create some kind of identifier for that file and submit it to a persistent background queue for processing. You can use polling / long polling / websockets / SignalR to notify the client of progress.

Note that ASP.NET assumes that your process can be recycled if there are no outstanding requests, so the processing should be done by something other than your MVC controller. Like a Win32 service or Azure worker role.

Also, if this is for a site in a web farm, your persistent queue needs to be reachable from all web servers. And if you're hosting on an Azure web role, it can't be on disk (or MSMQ) because Azure will recycle your entire computer - in that case, it should be in Azure storage or the Service Bus.

like image 124
Stephen Cleary Avatar answered Jan 02 '23 04:01

Stephen Cleary


not sure what your exact requirements are but you could use an off the shelf upload control that could fit the bill such as the Telerik MVC upload extension and its free!

http://demos.telerik.com/aspnet-mvc/upload

or you could custom roll your own one; what I am thinking is when the user uploads the xml file/files then the controller method could put them into a background queue for processing or hand it off to a windows service that takes care of the processing and returns a ticket number(s) to the client. Expose another webservice/restful service that gives progress/status of the files uploading queried via a ticket number and have your client/website query the webservice in intervals to get progress and show progress to user.

Think you might find it easier to use existing upload extensions for MVC so dont need to reinvent the wheel, my thoughts any way :-)

like image 39
Ricky Gummadi Avatar answered Jan 02 '23 03:01

Ricky Gummadi