Hangfire Discussion

How to debug Background Job?

Tags: #<Tag:0x00007f84f9155c68>


I am newbie to Hangfire and configured my Hangfire with Topshelf by following the post below:

I am trying to debug my Background Job although its running successfully but somehow the code is not hitting the breakpoint in debug mode.

This is the piece of code for my Scheduler class:

public override Expression<Action> GetExpression =>
    () => Execute(RuleApp.Name, FileIn, FileOut);

public static async Task Execute(string ruleAppName, 
    FileInput fileIn, FileOutput fileOut)
    // breakpoint not hitting here

This is the piece of code which runs inside the Windows Service Start method, where scheduler is the instance of my Scheduler class:

BackgroundJob.Schedule(scheduler.GetExpression, DelayInTimeSpan);

Looking forward for support on how to debug my background job.


Do you have more than 1 processing server (including your webapp which will be running a server if you instantiated a BackgroundJobServer)? If you enable the dashboard, look at it for the job and see if there are any details.

In terms of general debugging, if you only have 1 actual background server running and you are actively debugging it while it runs, your breakpoint will hit. If you are running it as an actual service locally and trying to debug, you might run into issues. Topshelf should allow you to run it as a console app locally and then deploy as a service.

Lastly, a probably unrelated item, you are passing in FileIn and FileOut objects into your scheduled job. I don’t believe these are standard .NET classes and without context I don’t know what they are, but I would highly recommend only passing primitives as per the Best Practices. If these objects are any kind of stream, this won’t work as they will execute completely different threads, contexts, and possibly machines. What Hangfire does to your inputs is serializes it to JSON, stores in your storage, and then when it get processed it gets deserialized and executed. If the classes you are using do not support this, you will have issues.