I am trying to integrate Hangfire into our ASP.Net Core application and I am getting a “Value cannot be null. Parameter name: factory”. Based on the error it seems that hangfire is unable to serialize the Logger class. How should I go about resolving the issue?
Startup.cs:
public Startup(IConfiguration configuration, ILogger logger)
{
Configuration = configuration;
this.Logger = logger;
}
public ILogger Logger;
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
#region Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString(“HangfireConnectionString”), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
}));
services.AddHangfireServer();
#endregion
public void Configure(IApplicationBuilder iApplicationBuilder, IBackgroundJobClient iBackgroundJobClient, IHostingEnvironment iHostingEnvironment)
{
#region Hangfire integration
iApplicationBuilder.UseHangfireDashboard();
iBackgroundJobClient.Enqueue(() => Logger.LogWarning($“Fire and forget jobs”));
RecurringJob.AddOrUpdate(() =>
Logger.LogWarning($“Recurring jobs”), Cron.Daily);
var jobId = BackgroundJob.Schedule(() =>
Logger.LogWarning($“Delayed jobs”), TimeSpan.FromDays(7));
BackgroundJob.ContinueWith(jobId, () => Logger.LogWarning(“Jobs are executed when its parent job has been finished”));
#endregion Hangfire integration
StackTrace:
Failed
Can not change the state to ‘Enqueued’: target method was not found.
System.ArgumentNullException
Value cannot be null. Parameter name: factory
System.ArgumentNullException: Value cannot be null.
Parameter name: factory
at Microsoft.Extensions.Logging.Logger1..ctor(ILoggerFactory factory) at lambda_method(Closure , Object[] ) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor
1 creator, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Hangfire.Common.SerializationHelper.Deserialize(String value, Type type, SerializationOption option)
at Hangfire.Storage.InvocationData.DeserializeArgument(String argument, Type type)
— End of stack trace from previous location where exception was thrown —
at Hangfire.Storage.InvocationData.DeserializeArgument(String argument, Type type)
at Hangfire.Storage.InvocationData.DeserializeArguments(MethodInfo methodInfo, String[] arguments)
at Hangfire.Storage.InvocationData.DeserializeJob()