为ASP.NET应用程序实现后台服务的最佳方法是什么?

时间:2022-10-22 08:52:37

My ASP.NET application needs a number of supporting services to run periodically in the background. For example:

我的ASP.NET应用程序需要许多支持服务才能在后台定期运行。例如:

  • I need to query the database (or cache) every 1-5 minutes, identify overdue work items and notify users by email
  • 我需要每1-5分钟查询一次数据库(或缓存),识别过期的工作项并通过电子邮件通知用户

  • I need to generate nightly reports that are then emailed to subscribers
  • 我需要生成每晚报告,然后通过电子邮件发送给订阅者

  • Plus other system/non-user admin tasks
  • 加上其他系统/非用户管理任务

What is the best way to implement these services? Should I include them as part of the web application, starting an instance of each 'service' in Application_Start()? Or create them as actual standalone services? If they were part of the web application I can potentially utilize my cached data store, but are there any downsides to this approach?

实施这些服务的最佳方式是什么?我应该将它们作为Web应用程序的一部分包含在内,在Application_Start()中启动每个“服务”的实例吗?或者将它们创建为实际的独立服务?如果他们是Web应用程序的一部分,我可以利用我的缓存数据存储,但这种方法有任何缺点吗?

Thanks for any advice.

谢谢你的建议。

5 个解决方案

#1


6  

This year I needed to implement a single task to cache some data from a database, It had to verify a changing code every n minutes, I found this very nice article;

今年我需要实现一个任务来缓存数据库中的一些数据,它必须每隔n分钟验证一次更改的代码,我发现这篇非常好的文章;

Simulate a Windows Service using ASP.NET to run scheduled jobs

使用ASP.NET模拟Windows服务以运行预定作业

I did it that way and it worked, but as I told you was it was only a single task, but if you see the example above you can see that it performs several tasks.

我这样做了它并且它有效,但正如我告诉你的那只是一项任务,但是如果你看到上面的例子,你可以看到它执行了几项任务。

As you could see, in the cache you can perform a task at any time and on intervals, the nightly task could be done at midnight, the others every n minutes or hours.

正如您所看到的,在缓存中您可以随时和间隔执行任务,夜间任务可以在午夜完成,其他任务可以每n分钟或几小时完成。

Hope it helps.

希望能帮助到你。

#2


1  

-- For the nightly report generation I would look into SQL Reporting Services because they have some native subscription services that allow you to e-mail reports to users with virtual no custom code.

- 对于夜间报告生成,我会研究SQL Reporting Services,因为它们具有一些本机订阅服务,允许您通过虚拟的自定义代码向用户发送电子邮件报告。

-- For the overdue work item notifications you could either use SSRS (mentioned above) by sending them a report with their overdue items or, if you're running SQL 2k5, use Notification Services.

- 对于逾期工作项通知,您可以使用SSRS(如上所述)通过向其发送包含其逾期项的报告,或者,如果您运行的是SQL 2k5,则使用Notification Services。

#3


1  

I do spinoff worker threads and threads that do things periodically in my ASP.NET application and I haven't had any problem.

我在我的ASP.NET应用程序中定期执行工作线程和线程,我没有遇到任何问题。

I don't just run the app in house - others download it and run it on their sites, or run it at hosting companies, so doing things without requiring that a service be installed makes the deployments easier.

我不只是在内部运行应用程序 - 其他人下载并在其网站上运行它,或者在托管公司运行它,因此无需安装服务即可完成部署,从而简化部署。

#4


1  

A timer combined with a background worker has worked well for me:

与背景工作者结合的计时器对我来说效果很好:

Timer timer = new Timer(intervalSeconds * 1000)
timer.Elapsed += delegate
{
    // do the meat
};
timer.Start();

Keep in mind that you won't have access to the HttpContext and that the site may shut down due to inactivity.

请记住,您将无法访问HttpContext,并且该网站可能因不活动而关闭。

#5


0  

Honestly I've never been a fan of trying to simulate a windows service inside an ASP.net process, especially when you consider how easy it is to create a Windows service. Most of the attempts that I've seen at doing so have been full of problems.

老实说,我从未想过尝试在ASP.net进程中模拟Windows服务,特别是考虑到创建Windows服务是多么容易。我所看到的大部分尝试都充满了问题。

#1


6  

This year I needed to implement a single task to cache some data from a database, It had to verify a changing code every n minutes, I found this very nice article;

今年我需要实现一个任务来缓存数据库中的一些数据,它必须每隔n分钟验证一次更改的代码,我发现这篇非常好的文章;

Simulate a Windows Service using ASP.NET to run scheduled jobs

使用ASP.NET模拟Windows服务以运行预定作业

I did it that way and it worked, but as I told you was it was only a single task, but if you see the example above you can see that it performs several tasks.

我这样做了它并且它有效,但正如我告诉你的那只是一项任务,但是如果你看到上面的例子,你可以看到它执行了几项任务。

As you could see, in the cache you can perform a task at any time and on intervals, the nightly task could be done at midnight, the others every n minutes or hours.

正如您所看到的,在缓存中您可以随时和间隔执行任务,夜间任务可以在午夜完成,其他任务可以每n分钟或几小时完成。

Hope it helps.

希望能帮助到你。

#2


1  

-- For the nightly report generation I would look into SQL Reporting Services because they have some native subscription services that allow you to e-mail reports to users with virtual no custom code.

- 对于夜间报告生成,我会研究SQL Reporting Services,因为它们具有一些本机订阅服务,允许您通过虚拟的自定义代码向用户发送电子邮件报告。

-- For the overdue work item notifications you could either use SSRS (mentioned above) by sending them a report with their overdue items or, if you're running SQL 2k5, use Notification Services.

- 对于逾期工作项通知,您可以使用SSRS(如上所述)通过向其发送包含其逾期项的报告,或者,如果您运行的是SQL 2k5,则使用Notification Services。

#3


1  

I do spinoff worker threads and threads that do things periodically in my ASP.NET application and I haven't had any problem.

我在我的ASP.NET应用程序中定期执行工作线程和线程,我没有遇到任何问题。

I don't just run the app in house - others download it and run it on their sites, or run it at hosting companies, so doing things without requiring that a service be installed makes the deployments easier.

我不只是在内部运行应用程序 - 其他人下载并在其网站上运行它,或者在托管公司运行它,因此无需安装服务即可完成部署,从而简化部署。

#4


1  

A timer combined with a background worker has worked well for me:

与背景工作者结合的计时器对我来说效果很好:

Timer timer = new Timer(intervalSeconds * 1000)
timer.Elapsed += delegate
{
    // do the meat
};
timer.Start();

Keep in mind that you won't have access to the HttpContext and that the site may shut down due to inactivity.

请记住,您将无法访问HttpContext,并且该网站可能因不活动而关闭。

#5


0  

Honestly I've never been a fan of trying to simulate a windows service inside an ASP.net process, especially when you consider how easy it is to create a Windows service. Most of the attempts that I've seen at doing so have been full of problems.

老实说,我从未想过尝试在ASP.net进程中模拟Windows服务,特别是考虑到创建Windows服务是多么容易。我所看到的大部分尝试都充满了问题。