使用 BackgroundJobServer¶
在一个web框架中,基础 (但不简单 – 参阅下节) 的使用方法是调用与主机无关的 BackgroundJobServer 类中的 Start 和 Dispose 方法(参阅 前一章)。
尽可能释放服务器实例
在某些Web应用程序框架中,如果何时调用 Dispose 方法不是很清楚的情况下,您可以 像这样 调用(但可能非 正确关闭 )。
例如,在ASP.NET应用程序中,调用 start/dispose 方法的最佳方式是在 global.asax.cs 文件中:
- using System;
- using System.Web;
- using Hangfire;
- namespace WebApplication1
- {
- public class Global : HttpApplication
- {
- private BackgroundJobServer _backgroundJobServer;
- protected void Application_Start(object sender, EventArgs e)
- {
- GlobalConfiguration.Configuration
- .UseSqlServerStorage("DbConnection");
- _backgroundJobServer = new BackgroundJobServer();
- }
- protected void Application_End(object sender, EventArgs e)
- {
- _backgroundJobServer.Dispose();
- }
- }
- }
原文:
http://hangfirezh.zhs.press/background-processing/processing-jobs-in-web-app.html