I can't register a RecurringJob with IConfiguration dependency (Serialization error)

recurring
aspnetcore
Tags: #<Tag:0x00007f8b98899420> #<Tag:0x00007f8b988992e0>

#1

Hi,

I am trying to register a recurringjob that depends on Microsoft.Extensions.Configuration.IConfiguration.

This is how I register the job:

public void Configure(..., IConfiguration configuration)
{
    ...
    RecurringJob.AddOrUpdate<RecurrentJobs>(x => x.DoRemindersJob(configuration), "*/5 * * * *");
    ...
}

This is the simple DoRemindersJob method:

namespace WebAPI.Helpers
{
    public class RecurrentJobs
    {
        public RecurrentJobs()
        {
        }

        public void DoRemindersJob(IConfiguration configuration)
        {
            Console.WriteLine("Starting reminder job");
            string connectionString = configuration.GetConnectionString("MySqlConnection");
            Console.WriteLine(connectionString);
        }
 }

This throws an exception when executed like this one:

Newtonsoft.Json.JsonSerializationException: Error getting value from ‘UsePollingFileWatcher’ on ‘Microsoft.Extensions.FileProviders.PhysicalFileProvider’.
—> System.InvalidOperationException: Cannot modify UsePollingFileWatcher once file watcher has been initialized.
at Microsoft.Extensions.FileProviders.PhysicalFileProvider.get_UsePollingFileWatcher()
at lambda_method(Closure , Object )
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
— End of inner exception stack trace —
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)
at Hangfire.Common.SerializationHelper.Serialize(Object value, Type type, SerializationOption option)
at Hangfire.Storage.InvocationData.SerializeArguments(MethodInfo methodInfo, IReadOnlyList1 arguments) at Hangfire.Storage.InvocationData.SerializeJob(Job job) at Hangfire.RecurringJobEntity.GetChangedFields(Nullable1& nextExecution)
at Hangfire.RecurringJobEntity.IsChanged(IReadOnlyDictionary2& changedFields, Nullable1& nextExecution)
at Hangfire.RecurringJobManager.AddOrUpdate(String recurringJobId, Job job, String cronExpression, RecurringJobOptions options)
at Hangfire.RecurringJobManagerExtensions.AddOrUpdate(IRecurringJobManager manager, String recurringJobId, Job job, String cronExpression, TimeZoneInfo timeZone, String queue)
at Hangfire.RecurringJob.AddOrUpdate[T](Expression1 methodCall, String cronExpression, TimeZoneInfo timeZone, String queue) at WebApi.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpContextAccessor httpAccesor, PathsService _pathsService, IConfiguration configuration) in /Users/hexdump/Development/Projects/dotnet/vtc-webapi/WebAPI/Startup.cs:line 314 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error getting value from 'UsePollingFileWatcher' on 'Microsoft.Extensions.FileProviders.PhysicalFileProvider'. ---> System.InvalidOperationException: Cannot modify UsePollingFileWatcher once file watcher has been initialized. at Microsoft.Extensions.FileProviders.PhysicalFileProvider.get_UsePollingFileWatcher() at lambda_method(Closure , Object ) at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings) at Hangfire.Common.SerializationHelper.Serialize(Object value, Type type, SerializationOption option) at Hangfire.Storage.InvocationData.SerializeArguments(MethodInfo methodInfo, IReadOnlyList1 arguments)
at Hangfire.Storage.InvocationData.SerializeJob(Job job)
at Hangfire.RecurringJobEntity.GetChangedFields(Nullable1& nextExecution) at Hangfire.RecurringJobEntity.IsChanged(IReadOnlyDictionary2& changedFields, Nullable1& nextExecution) at Hangfire.RecurringJobManager.AddOrUpdate(String recurringJobId, Job job, String cronExpression, RecurringJobOptions options) at Hangfire.RecurringJobManagerExtensions.AddOrUpdate(IRecurringJobManager manager, String recurringJobId, Job job, String cronExpression, TimeZoneInfo timeZone, String queue) at Hangfire.RecurringJob.AddOrUpdate[T](Expression1 methodCall, String cronExpression, TimeZoneInfo timeZone, String queue)
at WebApi.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpContextAccessor httpAccesor, PathsService _pathsService, IConfiguration configuration) in /Users/hexdump/Development/Projects/dotnet/vtc-webapi/WebAPI/Startup.cs:line 314
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at WebApi.Program.Main(String[] args) in /Users/hexdump/Development/Projects/dotnet/vtc-webapi/WebAPI/Program.cs:line 20

I think I’m doing same thing documentation states and I am really stuck at this. Could anybody help please?

Cheers!


#2

I question why you are trying to serialize the whole IConfiguration object. Why not just use the connectionstring as the method’s parameter instead? It would mean less overhead and be easier to read the code.

public void Configure(..., IConfiguration configuration)
{
    ...
    RecurringJob.AddOrUpdate<RecurrentJobs>(x => x.DoRemindersJob(configuration.GetConnectionString("MySqlConnection")), "*/5 * * * *");
    ...
}
public void DoRemindersJob(string mySqlConnection)
{
    Console.WriteLine("Starting reminder job");
    string connectionString = mySqlConnection;
    Console.WriteLine(connectionString);
}

If you need more a lot more parameters than just the connectionstring, create a basic serializable object with the properties you need.