Running a short-lived background task from MVC - c#

I am coding an ASP.NET MVC 3 app. When a user logs in I need to check a remote system and get the latest data for that user from the system. This task will take approx 15 seconds.
The user should be able to enter my app straight after their login (not have to wait for 15s for the remote call!). When the remote call completes the users local information will be updated.
I was thinking of using a thread to do this, creating it after they have logged in and letting it run its course. However, after reading around, I am concerned about recycling etc when working with threads in MVC. I would use an async controller, but I dont need to feedback to the user the state of this background process. Am I right to be concerned about threads, even if they are short-lived?

"...concerned about recycling..."
"...don't need to feedback to the user the state..."
"...short-lived..."
3 reasons why you should be using ThreadPool.QueueUserWorkItem.

Do not use "threads" in an web app. Let the server handle this by using "async" calls.
Otherwise you would have to setup a threadpool and que the slow request.

Related

updating aspx page from server

Currently I am working on a project where a web page will start a 3 hour data transfer process through the use of a web service. Basically a list of Id's are used to tell the web service to transfer an data object from one database to another.
When I start the process, I would like to allow the user to remain in control. He must be able to see how far the current process is, and be able press a cancel button to stop the data transfer. All this is run from an ASPX web page.
I have discovered that trying to run the process asynchronous does not allow me to update the user interface as I had hoped. Only when the entire process is finished Another problem I might face is that since the process takes a lot of time, the server objects might get refreshed at some point, which could cause me to lose my progress.
I am currently at the point where I am deciding whether or not to use the web server to process the data transfer. If I am, my current solution is to use tasks to run the process asynchronously, and use client side web calls (to the aspx page) to update the client interface. I am also implementing the IRegisteredObject interface for my work object. IRegisteredObject explanation
Any idea on how to best tackle this problem is most welcome. I really want to know if I'm heading in the right direction.
My suggestion would be for you to create a WCF service, which is not hosted within ASP.NET and to make your ASP.NET application call that service to trigger the long running job that performs the data transfer. Meanwhile your ASP.NET app notifies the user that it has triggered the job and you can expose other endpoints on your service which the ASP.NET application can query through a user request in order to extract and report progress.
The web server will only run one page at a time for each user, so if you want to communicate with the browser while the process is running, you can't run the progress from a request. You need to start it as an independent background thread, so that you can finish the request that started the process.
After that you can send requests to the server either to do polling for status of the process, or to control the process.
If possible you should run the process entirely outside of IIS, for example as a console application. That way you only have to keep track of the fact that there is a process running in the web application, for example putting that in a database so that it survives IIS recycling.

How to keep ASP.Net application running after closing the web browser?

I have a web application that do some work on database every specific time, so how can I keep it doing its work even I close the web browser?
Is using a Thread useful and will it work for me? What other available solutions?
You don't want to do that. A regular web server process isn't a background worker that runs forever, it replies on requests and it keeps some state. You don't want to let your thread end because of the application pool getting recycled, sessions time out, etc.
You have a few options, which you could use:
A Windows service (the most logical thing to do);
The new .NET 4.5.2 HostingEnvironment.QueueBackgroundWorkItem (which separates itself from the user session context. Note: for short-lived background tasks only! Since 'The AppDomain shutdown can only be delayed 90 seconds');
Windows Azure Web Jobs (for in hosted environments);
Task Scheduler.

Run an application from the IIS application pool

I have a console application which basically sends emails once per day.
The Windows server administrator disallows this technique and doesn't want to allow extra software on the computer (launched by a scheduled task or a service).
I've been asked to evaluate the possibility of redeveloping a part of the application and integrate it into the IIS application pool but I don't think IIS can do this. Is it possible ? If so, how ?
The only approach I've looked at so far is to redevelop it as a web application and launch a web page everyday with a scheduled task, but I'd like to avoid that.
Let's analyze your options:
Use task scheduler in your server to launch console app
Use task schedule**r in your server to **web service hosted in IIS
Have an IIS application running 100% of time in an infinite loop that that checks time every minute and if it happens to be the correct time send the emails
Have a windows service.
Use task scheduler in a different server to invoke
Analyzing each one of them:
KO: Your administrator does not want console apps and process is not isolated.
KO: You have process isolated but still you are installing a console app.
OK: Not very good for performance but your fulfills your admin conditions.
KO: Your admin does not want windows services.
??: Probably your admin will not want to use an extra server
Proposed solution: As you can see only options 3 and 5 might pass the filter. Ask him
Correct solution I did similar things in the past and went for option 2. You have to convince your admin that solution 3 is a bad idea and probably 5 is already out of the question. When he had to choose the lesser of the evils option 2 is the best :-)
NOTE: You don't mention it but in case you have a SQL Server or similar you can store there an scheduled task too...
I had similar questions when I was moving from Apache servers (where it's dead easy to send a nightly email) to Windows (where you have options).
Clients have sometimes pushed me towards SQL Mail. It's not terrible. If your web app has a SQL backend, and it's okay to schedule things there, it's capable of sending emails when properly configured.
I don't think this is possible. With an IIS application you'd need something to trigger loading the application (call the web page). This itself would require a scheduled task.
You need to pound some sense into your administrator. Sorry.

Is there a way to have code run on a time schedule in an ASP.NET web app?

I have an ASP.NET web app providing access to a database on the server. What I need is a way to run code in the background on a given schedule that auto-updates the server database from another source.
I know how to do this in a windows app by adding a timer, linking up a function to the timer tick event and starting the timer. I do not know how to do this in a web app.
Is there a start-up event for a web app or somewhere where I can start this background process regardless of whatever any users are doing on the site?
You should not do this in an ASP.NET website - this is a major no-no. You are correct in thinking to use a timer on a background .exe. You should look into creating either a Windows Task (a console .exe executed by the server task timer), or a Windows Service. I would suggest the Windows Service as that is standard practice.
If you have access to the computer hosting your site I would write a little app that was run from the Task Scheduler.
The web server is not meant to handle long-running background tasks. It's the wrong tool.
If you dont have access to the hosting computer then I would suggest building some kind of interface whereby another computer rebuilt the database and uploaded it. I'm using the terms "interface" and "upload" in the loosest, broadest sense - apply your own definition.
I was searching for a solution myself couple of months ago, and even though I haven't found enough time to try it so far, I guess I can share the link with you. Maybe you'll find it helpful.
If yes, please, let me know here.
http://quartznet.sourceforge.net/
How to use Quartz.net with ASP.NET
you can use Windows Service or use Timer Control (In the Ajax Category)
Or
As other answers have stated, doing this full function - updating a database and scheduling it as an ASP.NET app is using the wrong tool for the job.
ASP.NET can be used to update a database - that's perfectly valid. Where it breaks down is in the timer. ASP.NET apps aren't meant to be long-running, which is necessary for the timer to work.
If you can do it, I'd strongly suggest using the approach others have suggested - a Windows Service or a Scheduled Task.
However, if you have no access to the actual server, other than to post ASP.NET code - you can't install a service and you can't set up a Windows app to run on a scheduled basis, here's an out-of-the box idea.
Set up a web service or ASPX page that does the update, and then call that page from a scheduled task on a machine you DO control.
So if this was at http://www.someserver.net/updatedb.aspx, there's no reason you can't set a scheduled task on your own PC to call that URL.
I'd consider this a last-ditch solution to be used only if you can't do one of the other options.
The global.asax.cs file has a method that is fired when your application starts: Application_Start. You can hook up your timer method in that event. Just beware, depending on how IIS configured, your app pool may shutdown. For example, if no one hits the site in 20 minutes for example. Just make sure if you HAVE to have this run every X minutes that you have IIS configured to ALWAYS be running and start your app. This is harder than it sounds. In the end, you may want to go with a regular windows scheduled task.

I need a notification system in a web application

I have a web application which our customer use it all day. Some times, i need to stop or restart application. When I do it, some customers' work is interrupted and this makes them nervous.
I want to make a notification system that, appear on application and inform customers about shutting down of system.
How can I make a system like that?
For notification you can use stackoverflow-style-notifications
You also can customize error page "HTTP Error 500-12 Application Restarting"
I think the way to go here is with AJAX. Make some kind of Polling system which would poll every 5 or so minutes for new 'messages' on the server. your C# would then check if there are new messages, return them to the AJAX and some jQuery could could display a box or a modal to alert the user something will happen soon.
On refreshing a page or going to a new page this ajax could then run too, not waiting for the timer to run out.
You can use a service on your network but there is another simple way...
there is a dos command which shows a message on specified user's screen... but this is worst way if ur program is not running on a small network...
The command is NET SEND but its no more available in windows vista and seven...
Here is the refrence: http://www.cezeo.com/tips-and-tricks/net-send-command/
You would put a message on a common page (home or login) a while before the update. Something like "At xxxxxpm this site will be down".
When the time comes you replace their access to the system (such as the login page) with a page stating its down, you'll have to wait. You would either place the new message page with that of the same name as the login, or configure the server to redirect automatically. This is how the major banks manage it.

Categories