Long-running background process in ASP.NET - Application_Start or separate process?

后端 未结 2 572
轮回少年
轮回少年 2020-12-10 14:48

I\'m developing a .NET 4 application that requires a backend worker thread to be running. This thread consists mostly of the following code:

while (true) {
          


        
相关标签:
2条回答
  • 2020-12-10 15:02

    I would strongly opt for the Windows Service if possible. Background threading in ASP.NET comes with a lot of baggage.

    1. The lifetime of your background process is at the mercy of IIS. If IIS decides its time to recycle the App Pool, your background process will restart. If IIS decides to stop the App Pool due to inactivity, your background process will not run.
    2. If IIS is configured to run as a Web Garden (multiple processes per AppPool), then your background thread could run more than once.
    3. Later on, if you decide to load balance your website (multiple servers running the site), then you may have to change your application to ensure the background threading is only happening on one server).

    And plenty more.

    0 讨论(0)
  • 2020-12-10 15:12

    Consider something simple like Hangfire and then think about the design points in this related answer.

    0 讨论(0)
提交回复
热议问题