ASP .NET MVC - Should I run a background timer

2020-07-27 03:48发布

问题:

I need to do some processing in the background when the website is running. Every 30 minutes I need to run a piece of code that interacts with the database.

Would it be okay to use a timer that executes the code every 30 minutes or is there a better way of doing this? If timer is okay to implement, I'm thinking to implement it in the Application_Start() function in the Global.asax folder.

From what I've read, a timer wouldn't cause performance issues because for the majority of the time it's not actually running any code.

I've been searching but couldn't find anything MVC related. Not sure if that matters.

回答1:

If you are are really set on building it into asp.net take a look at Haacked's article The Dangers of Implementing Recurring Background Tasks In ASP.NET.

He outlines the dangers and some ways you can combat them, otherwise as others suggested a service/schedule task is a better solution.

Alternatively if you go down the windows service route take a look at TopShelf its a framework that lets you easily run services as console apps in development while still allowing them to be easily installed on the server