从 1.4 版本开始,GlobalConfiguration 类是配置 Hangfire 的首选方式。这是一些方法的入口点,包括来自第三方存储实现或其他扩展的方法。使用很简单,只需在应用程序初始化类中包含 Hangfire 命名空间,并发现GlobalConfiguration.Configuration 属性的扩展方法即可。
例如,在 ASP.NET 应用程序中,您可以将初始化逻辑放置在 Global.asax.cs 文件中:
using Hangfire;public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){// Storage is the only thing required for basic configuration.// Just discover what configuration options do you have.GlobalConfiguration.Configuration.UseSqlServerStorage("<name or connection string>");//.UseActivator(...)//.UseLogProvider(...)}}
对于基于 OWIN 的应用程序(ASP.NET MVC 、 Nancy 、 ServiceStack 、 FubuMVC 等),将配置行放置在 OWIN Startup 类中。
using Hangfire;[assembly: OwinStartup(typeof(Startup))]public class Startup{public void Configuration(IAppBuilder app){GlobalConfiguration.Configuration.UseSqlServerStorage("<name or connection string>");}}
对于其他应用程序,将其放置在调用其他 Hangfire 方法之前的位置。
