We are leaving Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

How we can programmatically stop Hangfire server permanently

Hello dear all,

I would like to raise this as a question that how can we programmatically stop Hangfire server permanently, when the server is running (either .net/.net core) with,

  • Scheduled console application(exe)
  • Web API/MVC Applications(hosted in IIS)
  • Web API/MVC Applications(docker containers/kubernetes)

Here is the code that we use to either gracefully shut down the Hangfire server

BackgroundJobServer _backgroundJobServer = new BackgroundJobServer(JobStorage.Current);

_backgroundJobServer.SendStop();
_backgroundJobServer.WaitForShutdownAsync(new CancellationToken()).GetAwaiter();
_backgroundJobServer.Dispose();

or

var monitoringApi = JobStorage.Current.GetMonitoringApi();
foreach (ServerDto server in monitoringApi.Servers())

  • {*
    JobStorage.Current.GetConnection().RemoveServer(server.Name);
    }