using Hangfire; using Hangfire.SqlServer; using HangfireApi.Controllers; var builder = WebApplication.CreateBuilder(args); builder.Services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions { CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), QueuePollInterval = TimeSpan.Zero, UseRecommendedIsolationLevel = true, DisableGlobalLocks = true })); builder.Services.AddControllers(); builder.Services.AddHangfireServer(); builder.Services.AddAuthorization(); builder.Services.AddSingleton(provider => provider.GetRequiredService().GetService(typeof(JobStorage)) as JobStorage ?? throw new InvalidOperationException()); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.MapControllers(); app.MapHangfireDashboard(); await ((IApplicationBuilder)app).ApplicationServices.GetRequiredService().RunJobs(); await app.RunAsync();