使用 BackgroundJobServer¶

在一个web框架中,基础 (但不简单 – 参阅下节) 的使用方法是调用与主机无关的 BackgroundJobServer 类中的 StartDispose 方法(参阅 前一章)。

尽可能释放服务器实例

在某些Web应用程序框架中,如果何时调用 Dispose 方法不是很清楚的情况下,您可以 像这样 调用(但可能非 正确关闭 )。

例如,在ASP.NET应用程序中,调用 start/dispose 方法的最佳方式是在 global.asax.cs 文件中:

  1. using System;
  2. using System.Web;
  3. using Hangfire;
  4.  
  5. namespace WebApplication1
  6. {
  7. public class Global : HttpApplication
  8. {
  9. private BackgroundJobServer _backgroundJobServer;
  10.  
  11. protected void Application_Start(object sender, EventArgs e)
  12. {
  13. GlobalConfiguration.Configuration
  14. .UseSqlServerStorage("DbConnection");
  15.  
  16. _backgroundJobServer = new BackgroundJobServer();
  17. }
  18.  
  19. protected void Application_End(object sender, EventArgs e)
  20. {
  21. _backgroundJobServer.Dispose();
  22. }
  23. }
  24. }

原文:

http://hangfirezh.zhs.press/background-processing/processing-jobs-in-web-app.html