长期运行在ASP.NET后台进程 - 的Application_Start或单独的进程?进程、后台、ASP、NET

2023-09-03 00:28:28 作者:难免孤独

我正在开发一个.NET 4的应用程序需要在运行一个后台工作线程。此线程主要是由以下几code:

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) {
    //Check stuff in database
    //Do stuff
    //write to database / filesystem
    Thread.sleep(60000)
}

在ASP.NET应用程序仅仅是一个前端数据库。

The ASP.NET app is just a frontend for the database.

我的问题是各地,其中最好的地方,把这个工人循环会。看来我的眼前两个选择是(1)从的Application_Start 方法旋转它,就让它运行,或(2)在单独的进程将其捆( Windows服务?)

My question is around where the best place to put this worker loop would be. It seems my immediate two choices would be (1) to spin it off from the Application_Start method, and just let it run, or (2) bundle it in a separate process (Windows service?)

(1)显然需要在ASP.NET code一些逻辑来检查它仍在运行,为IIS可能会杀了它。这也是很整齐,整个应用程序逻辑于一体,易于部署的包。 (2)更加分离,但感觉很多混乱。

(1) would obviously need some logic in the ASP.NET code to check it's still running, as IIS might kill it. It's also quite neat in that the whole application logic is in one, easily deployable package. (2) is much more segregated, but feels a lot messier.

什么是最好的方法呢?

推荐答案

我如果可能的话会强烈选择的Windows服务。背景线程在ASP.NET comes与很多行李的。

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

您的后台进程的生命周期是在IIS的摆布。如果IIS决定它的时间来回收应用程序池,你的后台进程将重新启动。如果IIS决定停止应用程序池由于不活动,你的后台程序将无法运行。 如果IIS是(每程序池的多个进程)配置为Web园运行,那么你的后台线程可以运行一次以上。 后来,如果你决定要负载平衡你的网站(该站点运行多个服务器),那么你可能需要更改你的应用程序,以确保后台线程只发生在一台服务器上)。

,还有更多。

 
精彩推荐
图片推荐