How to Disable hangfire when using Azure Web Apps and deployment slots

We use Azure web apps.
We have a Staging & Production Slot
When our app is on Staging how can we disable Hangfire?

What we do is add an app settings to the web.config indicating that Hangfire initialization should not occur. In the code we check the app settings and either initialize or not. Then we create a transform to remove the setting when compiled in release mode. It works like a charm.

  • Michael
1 Like

Hi! I am using swap on my staging and production slot. I am looking to use dynamic Hangfire queue names to solve this specific problem. I was wondering if you would have a better solution to this?

I have written a small writeup for this issue:

https://ryandavie.com.au/notes/hangfire-deployment-slots

@RyanDavie i have check this- https://ryandavie.com.au/notes/hangfire-deployment-slots

@Serena

If you run Hangfire on an Azure App Service with deployment slots, you’ll find that you have multiple Hangfire server instances running where you only wanted one.

One solution is to add a Staging configuration value to your slots. Go to the app service > Deployment Slots > {your-staging-slot} > Configuration:

You need to check the Deployment slot setting checkbox, which will mean this setting is retained on the staging instance after a swap occurs.

Now, in your startup you can skip Hangfire server registration if you are in a staging instance:

// We don't want staging instances taking hangfire jobs
    if (!configuration.Staging)
    {
        builder.Services.AddHangfireServer();
    }

You now need to add an AppConfig
false configuration value to your production slot (even if your configuration implicitly knows it is false). That’s because a Deployment slot setting will trigger an application restart, so your production instance will be restarted after it is moved to the staging slot, removing it from your Hangfire server list.

When you swap slots, you will briefly see a second server appear. Then, when the old production instance restarts it will be removed from the list.

1 Like