Common/HangfireExtensions.cs
Код: Выделить всё
public static class HangfireExtensions
{
public static IServiceCollection AddHangfireServices(this IServiceCollection services, string mongoConnectionString, string dbName, string projectName)
{
GlobalJobFilters.Filters.Add(new AppJobFilterAttribute(projectName));
services.AddHangfire(config =>
{
config.UseMongoStorage(mongoConnectionString, dbName, new MongoStorageOptions
{
MigrationOptions = new MongoMigrationOptions
{
MigrationStrategy = new MigrateMongoMigrationStrategy(),
BackupStrategy = new NoneMongoBackupStrategy()
},
CheckConnection = true,
CheckQueuedJobsStrategy = CheckQueuedJobsStrategy.TailNotificationsCollection
});
});
services.AddHangfireServer(options =>
{
options.Queues = new[] { Constants.AppNumber.ToString() };
});
return services;
}
}
Код: Выделить всё
Recurring job 'AJobAsync' can't be scheduled due to an error and will be disabled.
Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details.
---> System.IO.FileNotFoundException: Could not load file or assembly 'A.API, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
A. API/Program.cs
Код: Выделить всё
builder.Services.AddHangfireServices(connectionString, hangfireDatabase, "A.API");
app.UseHangfireDashboard();
var jobService = app.Services.GetRequiredService();
RecurringJobs.Initialize(jobService);
Код: Выделить всё
public static class RecurringJobs
{
public static void Initialize(IJobService service)
{
RecurringJob.RemoveIfExists(nameof(service.AJobAsync));
RecurringJob.AddOrUpdate(
recurringJobId: nameof(service.AJobAsync),
methodCall: () => service.AJobAsync(),
cronExpression: "0 */1 * ? * *", // Every 1 minute
queue: Constants.AppNumber.ToString()
);
}
}
Код: Выделить всё
public interface IJobService
{
Task AJobAsync();
}
Когда эти два проекта выполняются одновременно, я получаю упомянутую выше ошибку, поскольку он пытается выполнить задания, определенные в них обоих. Моя просьба состоит в том, чтобы только проект A мог выполнять задания, определенные в проекте A, и только проект B должен выполнять задания, определенные в проекте B.
Подробнее здесь: https://stackoverflow.com/questions/790 ... age-in-net