Starts in:
3 DAYS
1 HR
18 MIN
57 SEC
Starts in:
3 D
1 H
18 M
57 S
Open Closed

Hangfire: Add custom jsonconverter for specific jobs #667


User avatar
0
BassaSolutions created
  • ABP Framework version: v3.3.1

Good morning,

I have hangfire job parameters that needs a custom serializer to be serialized.

For newtonsoft, I can add it like this:

    var settings = new JsonSerializerSettings {Formatting = Formatting.Indented};
    settings.Converters.Add(new LhirModelsJsonConverter());
    var serializedData = JsonConvert.SerializeObject(bundle, settings);

How is it possible to define this custom serializer for a specific job, so that it is automatically used for serialization and deserialization of its arguments? I do not want to manually serialize the data before each insert and deserialize on the worker.

I use hangfire with the abp backgroundjob interface:

  [BackgroundJobName("lhirInsert")]
  public class FhirInsertArgs
  {
      public LhirData LhirData { get; set; }
  }
    _backgroundJobManager.EnqueueAsync(new LhirInsertArgs
    {
        LhirData = serializedData
    });

Best regards and thank you for your help, Kevin


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    There is no function related to JSON serialization in abp's hangfire.

    Maybe you can create a service to handle JSON serialization/deserialization.

  • User Avatar
    0
    BassaSolutions created

    Yes I have a service to deal with it. Now the problem is just that whoever uses the queue has to remember to do it with the service, and any errors will only occur on runtime.

    And ABP is wrapping the jobs in such a way that I found no solution to extend it?

    So best is the best way to implement the hangfire queuing logic by myself instead of using the ABP job library?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can consider replacing the services in the Volo.Abp.BackgroundJobs.HangFire package.

    https://github.com/abpframework/abp/tree/dev/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire

  • User Avatar
    0
    BassaSolutions created

    Hi, this guided me into the right direction, thank you. The solution was pretty simple: Hangfire has a helper function to define json serializers for it globally:

        var settings = new JsonSerializerSettings {Formatting = Formatting.Indented};
        settings.Converters.Add(new LhirModelsJsonConverter());
        GlobalConfiguration.Configuration.UseSerializerSettings(settings); // <-- from Hangfire.GlobalConfiguration
    

    This can be applied in OnApplicationInitialization(). The converter will then be correctly applied to all models of the defined base class.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06