从 1.4 版本开始,GlobalConfiguration 类是配置 Hangfire 的首选方式。这是一些方法的入口点,包括来自第三方存储实现或其他扩展的方法。使用很简单,只需在应用程序初始化类中包含 Hangfire 命名空间,并发现GlobalConfiguration.Configuration 属性的扩展方法即可。

    例如,在 ASP.NET 应用程序中,您可以将初始化逻辑放置在 Global.asax.cs 文件中:

    1. using Hangfire;
    2. public class MvcApplication : System.Web.HttpApplication
    3. {
    4. protected void Application_Start()
    5. {
    6. // Storage is the only thing required for basic configuration.
    7. // Just discover what configuration options do you have.
    8. GlobalConfiguration.Configuration
    9. .UseSqlServerStorage("<name or connection string>");
    10. //.UseActivator(...)
    11. //.UseLogProvider(...)
    12. }
    13. }

    对于基于 OWIN 的应用程序(ASP.NET MVC 、 Nancy 、 ServiceStack 、 FubuMVC 等),将配置行放置在 OWIN Startup 类中。

    1. using Hangfire;
    2. [assembly: OwinStartup(typeof(Startup))]
    3. public class Startup
    4. {
    5. public void Configuration(IAppBuilder app)
    6. {
    7. GlobalConfiguration.Configuration.UseSqlServerStorage("<name or connection string>");
    8. }
    9. }

    对于其他应用程序,将其放置在调用其他 Hangfire 方法之前的位置。