1

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?

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

Can't agree with this more. Do not do multi-threaded processing in a website or service. Offload the processing!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.