macpak
macpak

Reputation: 1301

ASP.NET Mvc + Threads

We've a requirement to create a web application, where data isretrieved from the FTP server and put to a database. There should be a thread which is responsible for pooling the FTP server, and if it finds new data, data is downloaded and inserted to the server. I was thinking about creating a new thread in Application_Start, however I'm not sure if it's a safe solution. Can anyone suggest a better solution?

Upvotes: 1

Views: 2919

Answers (3)

Mathias F
Mathias F

Reputation: 15891

Phill Haack wrote a nice article about using background tasks in ASP.NET. He explained why it is not a good idea (you might bring down the app pool and leave data in a corrupted state) and if you have to do it, how its done

http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

Upvotes: 4

Rich Turner
Rich Turner

Reputation: 10984

Why are you creating this as an MVC application? MVC is a great framework and approach when building websites, but is not suited to long-running background tasks like polling an FTP server and pushing data into SQL.

A good reason NOT to do this in MVC is that MVC sites are hosted in IIS which is built specifically to handle multiple incoming requests requiring a finite (and preferably very short) time duration. If a process hosted by IIS runs for too long, by default IIS will kill and restart the process. I've seen a lot of people get confused (and upset) when their "background task" keeps resetting before it completes. While you CAN configure IIS to keep long-running tasks running for longer, it's highly unadvisable to do so because this may well mask situations where an errant thread runs into a deadlock or gets stuck in a loop and keeps running for a long time.

For this task, I strongly recommend creating a Windows Service (examples on MSDN and CodeProject). A service runs in the background, can be auto-restarted (in case it crashes), and can be manually/programatically stopped, started and paused. These are all key capabilities of the kind of requirements you describe.

Upvotes: 1

BNL
BNL

Reputation: 7133

Do not do this directly from your website. Write a windows service to do this.

Upvotes: 4

Related Questions