0

I have a .NET 8 service which uses Hangfire library for async jobs. I have configured Hangfire with PostgreSQL database.

I need to check the state of a running job frequently for which I am using the PostgreSqlConnection.GetStateData method of the Hangfire.PostgreSql library in the following way:

private StateData GetJobState(string jobId)
{
    using (var connection = JobStorage.Current.GetConnection())
    {
        var state = connection.GetStateData(jobId);
        return state;
    }
}

I am getting the following exception intermittently in this method call:

Unable to find a default constructor to use for type Hangfire.PostgreSql.PostgreSqlMonitoringApi+SafeDictionary`2[System.String,System.String].
Path 'State', line 1, position 136.

StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract, Boolean& createdFromNonDefaultCreator)
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.CreateValueInternal(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 Hangfire.Common.SerializationHelper.Deserialize(String value, Type type, SerializationOption option)
--- End of stack trace from previous location ---
at Hangfire.Common.SerializationHelper.Deserialize(String value, Type type, SerializationOption option)
at Hangfire.Common.SerializationHelper.Deserialize[T](String value, SerializationOption option)
at Hangfire.Common.SerializationHelper.Deserialize[T](String value)
at Hangfire.PostgreSql.PostgreSqlConnection.GetStateData(String jobId)

What could be the reason for this exception? I don't understand why it is trying to deserialize the state data dictionary into type Hangfire.PostgreSql.PostgreSqlMonitoringApi+SafeDictionary2[System.String,System.String].

1 Answer 1

0

Why This Error Occurs

1. Serialization Conflict: The job state data was serialized as a regular dictionary but is being deserialized into Hangfires internal SafeDictionary type
2. Intermittent Nature: This typically happens when:
Reading state data written by a different Hangfire version
During certain job state transitions
When the serialization format doesn't match the deserialization expectations

Recommended Solutions

1. Upgrade Hangfire.PostgreSql (Best Solution)

dotnet add package Hangfire.PostgreSql --version latest

Many serialization issues have been fixed in newer versions.

2. Use the Monitoring API Instead The monitoring API is more stable for reading job states:

    private StateData GetJobState(string jobId)    {
        var monitoringApi = JobStorage.Current.GetMonitoringApi();
        var jobDetails = monitoringApi.JobDetails(jobId);
        return jobDetails?.History?.LastOrDefault(); // or filter by state
    }    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.