ASP.NET + WCF下长时间运行的线程进程

时间:2022-09-03 20:03:46

Duplicate
This is a close duplicate of Dealing with a longer running process in WCF. Please considering posting your answer to that one instead of this.

重复这是在WCF中处理更长时间运行的进程的密切复制。请考虑将答案发布到那个而不是这个。

Original Question
I'm implementing the business layer of an application that must run some background processes at scheduled times. The business layer is made up of several WCF services all running under the same web application.

原始问题我正在实现必须在预定时间运行某些后台进程的应用程序的业务层。业务层由几个WCF服务组成,所有这些服务都在同一Web应用程序下运行。

The idea is defining a set of 'tasks' that must be run at different times (eg. every 5 minutes, everyday at 23:00, etc). That wouldn't be hard to implement as a windows service, but the problem is, the tasks need access to data caches that are living in the services, so this 'scheduler' must run under the IIS context in order to access that data.

这个想法是定义一组必须在不同时间运行的“任务”(例如,每5分钟,每天23:00等)。这不是很难实现作为Windows服务,但问题是,任务需要访问生活在服务中的数据缓存,因此这个'调度程序'必须在IIS上下文中运行才能访问该数据。

What I'm doing currently is using a custom ServiceHostFactory in one of the WCF services which spawns a child thread and returns. The child thread sleeps and wakes up every X minutes to see if there are scheduled tasks and executes them. But I'm worried about IIS randomly killing my thread when it recycles the application pool or after some inactive time (eg. no activity on any of the WCF services, which listen for requests from the presentation layer). The thread must run uninterrupted regardless of activity on the services. Is this really possible?

我目前正在做的是在其中一个WCF服务中使用自定义ServiceHostFactory,它生成一个子线程并返回。子线程每隔X分钟休眠并唤醒,以查看是否有计划任务并执行它们。但我担心IIS在回收应用程序池时或在一些非活动时间之后随机杀死我的线程(例如,任何WCF服务都没有活动,它会监听来自表示层的请求)。无论服务上的活动如何,线程都必须不间断地运行。这真的有可能吗?

I have found an article by someone doing the same thing, but his solution seems to be pinging the server from the child thread itself regularly. Hopefully there is a better solution.

我找到了一个人做同样事情的文章,但他的解决方案似乎是定期从子线程本身ping服务器。希望有更好的解决方案。

2 个解决方案

#1


I have at some point implemented a Windows Service that would load a web page on a regular basis. The purpose of that was was that the site was hosting a Workflow Foundation runtime, and we wanted to ensure that the web application was brought back up after IIS recycling the application pool. Perhaps the same approach can be used in this case; have a service (or Scheduled Task in Windows; even simpler) run every x minutes and load a page that will check for tasks.

我在某个时候实现了一个Windows服务,它会定期加载一个网页。这样做的目的是该站点托管Workflow Foundation运行时,我们希望确保在IIS回收应用程序池后重新启动Web应用程序。也许在这种情况下可以使用相同的方法;有一个服务(或Windows中的计划任务;甚至更简单)每x分钟运行一次并加载一个将检查任务的页面。

#2


Is it a possibility to run either a Windows Service or place applications in the Windows Scheduler to execute methods in the WCF at certain times? Maybe use a BackgroundWorker inside the WCF. Another option would be for WCF to spawn other applications to do the business logic, passing the appropriate data, or pointers to the data in memory(unsafe).

是否可以运行Windows服务或在Windows Scheduler中放置应用程序以在特定时间执行WCF中的方法?也许在WCF中使用BackgroundWorker。另一种选择是WCF生成其他应用程序来执行业务逻辑,传递适当的数据或指向内存中的数据(不安全)。

#1


I have at some point implemented a Windows Service that would load a web page on a regular basis. The purpose of that was was that the site was hosting a Workflow Foundation runtime, and we wanted to ensure that the web application was brought back up after IIS recycling the application pool. Perhaps the same approach can be used in this case; have a service (or Scheduled Task in Windows; even simpler) run every x minutes and load a page that will check for tasks.

我在某个时候实现了一个Windows服务,它会定期加载一个网页。这样做的目的是该站点托管Workflow Foundation运行时,我们希望确保在IIS回收应用程序池后重新启动Web应用程序。也许在这种情况下可以使用相同的方法;有一个服务(或Windows中的计划任务;甚至更简单)每x分钟运行一次并加载一个将检查任务的页面。

#2


Is it a possibility to run either a Windows Service or place applications in the Windows Scheduler to execute methods in the WCF at certain times? Maybe use a BackgroundWorker inside the WCF. Another option would be for WCF to spawn other applications to do the business logic, passing the appropriate data, or pointers to the data in memory(unsafe).

是否可以运行Windows服务或在Windows Scheduler中放置应用程序以在特定时间执行WCF中的方法?也许在WCF中使用BackgroundWorker。另一种选择是WCF生成其他应用程序来执行业务逻辑,传递适当的数据或指向内存中的数据(不安全)。