I am using Microsoft.Extensions.Hosting
Right now using following code on program.cs
serviceCollection.AddHangfire((provider, globalConfig) =>
{
globalConfig
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseDefaultActivator()
.UseSqlServerStorage(context.Configuration.GetConnectionString("QuantumEComDbContext"));
});
JobStorage.Current = new SqlServerStorage(context.Configuration.GetConnectionString("QuantumEComDbContext"));
My Test Code
public class TestJob:ITestJob
{
private readonly ILogger _logger;
//public TestJob()
//{
//}
public TestJob(ILogger<TestJob> logger) => _logger = logger;
public void WriteLog(string logMesage) {
_logger.LogInformation($"Tets job");
}
}
public interface ITestJob
{
void WriteLog(string logMesage);
}
Calling from another class
foreach (var store in stores.Take(2))
{
BackgroundJob.Enqueue<ITestJob>(x => x.WriteLog("test"));
}
using (var server = new BackgroundJobServer())
{
Console.ReadLine();
}
But when running code getting following error.
024-08-16 11:50:22.7085 | WARN | Hangfire.AutomaticRetryAttribute | Failed to process the job '2': an exception occurred. Retry attempt 1 of 10 will be performed in 00:00:19.
System.MissingMethodException: Cannot dynamically create an instance of type 'Process.ITestJob'. Reason: Cannot create an instance of an interface.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at Hangfire.JobActivator.ActivateJob(Type jobType) in C:\projects\hangfire-525\src\Hangfire.Core\JobActivator.cs:line 48
at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type) in C:\projects\hangfire-525\src\Hangfire.Core\JobActivator.cs:line 82
at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context) in C:\projects\hangfire-525\src\Hangfire.Core\Server\CoreBackgroundJobPerformer.cs:line 60