Making ASP.NET application always running¶

By default, Hangfire Server instance in a web application will not be started until the first user hits your site. Even more, there are some events that will bring your web application down after some time (I’m talking about Idle Timeout and different app pool recycling events). In these cases your recurring tasks and delayed jobs will not be enqueued, and enqueued jobs will not be processed.

This is particulary true for smaller sites, as there may be long periods of user inactivity. But if you are running critical jobs, you should ensure that your Hangfire Server instance is always running to guarantee the in-time background job processing.

On-Premise applications¶

For web applications running on servers under your control, either physical or virtual, you can use the auto-start feature of IIS ≥ 7.5 shipped with Windows Server ≥ 2008 R2. Full setup requires the following steps to be done:


    - Enable automatic start-up for Windows Process Activation (WAS) and World Wide Web Publishing (W3SVC) services (enabled by default).
    - Configure Automatic Startup.aspx) for an Application pool (enabled by default).
    - Enable Always Running Mode for Application pool and configure Auto-start feature as written below.

Creating classes¶

First, you’ll need a special class that implements the IProcessHostPreloadClient interface. It will be called automatically by Windows Process Activation service during its start-up and after each Application pool recycle.

  1. public class ApplicationPreload : System.Web.Hosting.IProcessHostPreloadClient
  2. {
  3. public void Preload(string[] parameters)
  4. {
  5. HangfireBootstrapper.Instance.Start();
  6. }
  7. }

Then, update your global.asax.cs file as described below. It is important to call the Stop method of the BackgroundJobServer class instance, and it is also important to start Hangfire server in environments that don’t have auto-start feature enabled (for example, on development machines) also.

  1. public class Global : HttpApplication
  2. {
  3. protected void Application_Start(object sender, EventArgs e)
  4. {
  5. HangfireBootstrapper.Instance.Start();
  6. }
  7.  
  8. protected void Application_End(object sender, EventArgs e)
  9. {
  10. HangfireBootstrapper.Instance.Stop();
  11. }
  12. }

Then, create the HangfireBootstrapper class as follows. Since both Application_Start and Preload methods will be called in environments with auto-start enabled, we need to ensure that the initialization logic will be called exactly once.

  1. public class HangfireBootstrapper : IRegisteredObject
  2. {
  3. public static readonly HangfireBootstrapper Instance = new HangfireBootstrapper();
  4.  
  5. private readonly object _lockObject = new object();
  6. private bool _started;
  7.  
  8. private BackgroundJobServer _backgroundJobServer;
  9.  
  10. private HangfireBootstrapper()
  11. {
  12. }
  13.  
  14. public void Start()
  15. {
  16. lock (_lockObject)
  17. {
  18. if (_started) return;
  19. _started = true;
  20.  
  21. HostingEnvironment.RegisterObject(this);
  22.  
  23. GlobalConfiguration.Configuration
  24. .UseSqlServerStorage("connection string");
  25. // Specify other options here
  26.  
  27. _backgroundJobServer = new BackgroundJobServer();
  28. }
  29. }
  30.  
  31. public void Stop()
  32. {
  33. lock (_lockObject)
  34. {
  35. if (_backgroundJobServer != null)
  36. {
  37. _backgroundJobServer.Dispose();
  38. }
  39.  
  40. HostingEnvironment.UnregisterObject(this);
  41. }
  42. }
  43.  
  44. void IRegisteredObject.Stop(bool immediate)
  45. {
  46. Stop();
  47. }
  48. }

And optionally, if you want to map Hangfire Dashboard UI, create an OWIN startup class:

  1. public class Startup
  2. {
  3. public void Configuration(IAppBuilder app)
  4. {
  5. var options = new DashboardOptions
  6. {
  7. AuthorizationFilters = new[]
  8. {
  9. new LocalRequestsOnlyAuthorizationFilter()
  10. }
  11. };
  12.  
  13. app.UseHangfireDashboard("/hangfire", options);
  14. }
  15. }

Enabling Service Auto-start¶

After creating above classes, you should edit the global applicationHost.config file (%WINDIR%\System32\inetsrv\config\applicationHost.config). First, you need to change the start mode of your application pool to AlwaysRunning, and then enable Service AutoStart Providers.

Save only after all modifications

After making these changes, the corresponding application pool will be restarted automatically. Make sure to save changes only after modifying all elements.

  1. <applicationPools>
  2. <add name="MyAppWorkerProcess" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />
  3. </applicationPools>
  4.  
  5. <!-- ... -->
  6.  
  7. <sites>
  8. <site name="MySite" id="1">
  9. <application path="/" serviceAutoStartEnabled="true"
  10. serviceAutoStartProvider="ApplicationPreload" />
  11. </site>
  12. </sites>
  13.  
  14. <!-- Just AFTER closing the `sites` element AND AFTER `webLimits` tag -->
  15. <serviceAutoStartProviders>
  16. <add name="ApplicationPreload" type="WebApplication1.ApplicationPreload, WebApplication1" />
  17. </serviceAutoStartProviders>

Note that for the last entry, WebApplication1.ApplicationPreload is the full name of a class in your application that implements IProcessHostPreloadClient and WebApplication1 is the name of your application’s library. You can read more about this here.

There is no need to set IdleTimeout to zero – when Application pool’s start mode is set to AlwaysRunning, idle timeout does not working anymore.

Ensuring auto-start feature is working¶

If something went wrong…

If your app won’t load after these changes made, check your Windows Event Log by opening Control Panel → Administrative Tools → Event Viewer. Then open Windows Logs → Application and look for a recent error records.

The simplest method - recycle your Application pool, wait for 5 minutes, then go to the Hangfire Dashboard UI and check that current Hangfire Server instance was started 5 minutes ago. If you have problems – don’t hesitate to ask them on forum.

Azure web applications¶

Enabling always running feature for application hosted in Microsoft Azure is simpler a bit: just turn on the Always On switch on the Configuration page and save settings.

This setting does not work for free sites.
Making ASP.NET application always running - 图1

If nothing works for you…¶

… because you are using shared hosting, free Azure web site or something else (btw, can you tell me your configuration in this case?), then you can use the following ways to ensure that Hangfire Server is always running:


    - Use separate process to handle background jobs either on the same, or dedicated host.
    - Make HTTP requests to your web site on a recurring basis by external tool (for example, Pingdom).
    - Do you know any other ways? Let me know!

原文:

http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html