Код: Выделить всё
public static void RegisterApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddControllers();
//Fetching Connection string from APPSETTINGS.JSON
var ConnectionString = configuration.GetConnectionString("DbConnectionString");
// Services
services.AddScoped();
// Repositories
services.AddScoped();
var sqlStorage = new SqlServerStorage(ConnectionString);
var options = new BackgroundJobServerOptions
{
ServerName = "Test Server"
};
JobStorage.Current = sqlStorage;
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170).UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings().UseSqlServerStorage(ConnectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
RecurringJob.AddOrUpdate("Database Backup", service => service.RunJob(), Cron.Minutely);
services.AddHangfireServer();
}
Код: Выделить всё
public class CustomJobService : IBackGroundJobService
{
private readonly ICustomJob _customJobRepository;
public CustomJobService(ICustomJob customJobRepository)
{
_customJobRepository = customJobRepository;
}
public async Task RunJob()
{
await Task.Run(() => _customJobRepository.RunJob());
}
}
Код: Выделить всё
RecurringJob.AddOrUpdate("Database Backup", service => service.RunJob(), Cron.Minutely);
Подробнее здесь: https://stackoverflow.com/questions/791 ... ery-minute