Hang fire Background Server shutting down when using both Enqueue & Schedule

Hi,
[Latest version of HangFire Nuget Package used]

I have created a service in .NET 6 for taking care of one corn job. I need to execute a method right away and schedule it to execute again after some hours to take care of some business action based on user data.

As instructed in Program.cs I had registered HangFireService to use InMemoryStorage(as our service will be active all time) with HangFireServer as below,

builder.Services.AddHangfire(configuration => configuration.UseInMemoryStorage());

// Add the processing server as IHostedService
builder.Services.AddHangfireServer();

And in the service file, I have below code lines in the execute async method

using(var server = new BackgroundJobServer()){
  BackgroundJob.Enqueue(
    () => _logger.LogInformation("Fire-and-forget!"));

BackgroundJob.Schedule(
    () => _logger.LogInformation("Delayed!"),
    TimeSpan.FromHours(7));
}

When the service is running, the first LogInformation executes right away and able to see the message in log file. But the Schedule is not working even though
Jobid is creating and service is still up and running.

On analyzing the logs, found HangFire log saying background job server shutting down immediately after creating schedule job. I tried adding options for setting the shutdown time limit property as 7hours. But no luck.

As per poc, I need to execute a corn method right away and schedule the same corn method later. Am I missing anything here.

You’re creating two Hangfire server instances. The scheduled job is on the instance in the using block which will only exist as long as the using block is running, which won’t be 7 hours. You need to get the app running to get the configured background job to create the job. Also, you need a recurring job if you want something to run every X amount of time. That scheduled job would only run once, seven hours from now.