I am currently looking into making use of the async/await functionality to:
- submit a file.
- 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)
- 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…)
- 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?
Unfortunately,
asyncandawaitcannot do this. As I explain on my blog,asyncdoes 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.