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);
    }

Any Response me also I have the same question but the weird I have one hangfire server and JobStorage.Current.GetMonitoringApi().Servers() I get Two