App not starting with hangfire on Azure and dotnet 5

Hi,

I have a problem with Hangfire and dotnet 5 on Azure (Hangfire version: 1.7.18)

Here the Startup.cs file:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<EntitiesDb>(options =>
        {
            options.UseSqlServer(connectionString, sqlServerBuilder =>
            {
                sqlServerBuilder.UseNetTopologySuite();
            });
        });

        services.AddHangfire(configuration => configuration
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"),
                new SqlServerStorageOptions()
                {
                    SchemaName = "hangfire",
                    PrepareSchemaIfNecessary = true,
                }));

        // Add the processing server as IHostedService
        if (Configuration["GlobalConfiguration:HangfireEnvironment"] == "Production" ||
            Configuration["GlobalConfiguration:HangfireEnvironment"] == "Preproduction")
        {
            services.AddHangfireServer();
        }
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, GlobalConfiguration config)
    {
        app.UseHangfireDashboard("/hangfire", new DashboardOptions()
        {
            Authorization = new[] {new HangfireAuthorization()}
        });
    }
}

And the associated Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        IWebHostBuilder webHostBuilder = CreateWebHostBuilder(args);
        IWebHost webHost = webHostBuilder.Build();
        GlobalConfiguration.Configuration.UseActivator(new HangfireActivator(webHost.Services));

        using (var scope = webHost.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            services.GetRequiredService<ScheduledJobRunner>().ScheduleRecurrentJobs();
        }
        
        webHost.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options => options.Limits.MaxRequestBodySize = null) 
            .CaptureStartupErrors(true)
            .UseStartup<Startup>();
}

When I run this app locally I don’t have any problems, but on Azure I have the following error:

Application: w3wp.exe
CoreCLR Version: 5.0.20.51904
.NET Version: 5.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException: Unable to resolve service for type ‘Hangfire.IRecurringJobManager’ while attempting to activate ‘BackgroundTasks.ScheduledJobRunner’.

 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.<GetCallSite>b__0(Type type)
 at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
 at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
 at Program.Main(String[] args) in Program.cs:line 54

The ScheduledJobRunner class register diverse tasks on the provided IRecurringJobManager. But the implementation of IRecurringJobManager could not be found.

Anyone has encountered this error ?
Any help would be appreciated.

Thanks.

Did you ever find a solution for this problem? We´re experiencing similar problems.