I am working on an MVC 3 web applicaton (.NET Framework 4, IIS7) that acts as a front end for a custom ID card printer built on some legacy technology. Without going too far in to the details, when clicking “Print” the web app writes a row with the card info to the database and returns. Then a service on the back end picks up the row and handles the actual printing of the card. Once the back end service is finished, it updates the row with a status column indicating success or failure.
Right now the application POSTs the data (via jquery ajax) and once the row is written to the database the controller returns success which is displayed to the user. Because of the setup I’ve described, this may or may not actually reflect reality because the service or card printer may have a problem.
We have a new requirement to make the UI “wait” to update with or success or failure based on the update to the row from the service. It is already setup in an asynchronous way on the client side via the jquery ajax call, and I’m not worried about handling it on the front end. I’ve looked at SignalR and the like, and have this part of it covered.
What I am looking for is information (links & articles, not a solution) on how to deal with it correctly on the web server side. I do not want to block incoming requests to print while polling the database periodically from earlier requests. From a high level, I want to know what the best way is to set up the controller and necessary background process that will:
- Accept the request and write the row to the database
- set up a background polling process, then return control to ASP.net
- poll the database ever so often until the row is updated (or a timeout is reached)
- return an answer to the client
I am no expert on threading, but know enough to know that I’m in dangerous territory if this is done incorrectly. Any help is greatly appreciated.
I would comment but I do not have enough rep.
Why the focus on the server pushing to the UI? You say that the form writes the row the the database and then shows “Success” to the screen, which may not be the real status. At any point during the printing process can you query the database and find the latest status?
Is there something preventing you from having the UI update a section of the page with the latest status every X seconds? Have the UI pull the info instead of the server pushing it out.
I have a page that shows logs records from a database. Each day a process begins that writes more records to the log and it is useful to have a autorefreshing ability to monitor the process.
On that page I have a Ajax form that makes a request to the controller which outputs a new table in a partial view. This partial view is put into the
UpdateTargetIdof the Ajax form. For the autorefreshing I utilize the javascriptsetInterval()function to submit the Ajax form every X seconds. There are also additional callbacks that can be specified such asOnSuccesswhere you could check for specific return conditions.AjaxOptions class for Ajax.BeginForm
Example form
setInterval() from w3schools