Kjensen
Kjensen

Reputation: 12374

How to return a return value from Hangfire

I noticed this field in the Hangfire Dashboard for a succeeded job:

enter image description here

I would love to shove some data in there, to give me more information about what the job did.

How would I do that?

I have searched google, checked the Hangfire documentation etc, but with no luck - probably because "result" is a very tricky keyword to search for in this context.

Upvotes: 12

Views: 6913

Answers (2)

Anil
Anil

Reputation: 303

To programmatically get returned value, use IMonitoringApi.
E.g. Helper method to get List<TReturn>:

List<TReturn> GetReturnedItems<TReturn>(string jobId)
{
    IMonitoringApi jobMonitoringApi = JobStorage.Current.GetMonitoringApi();
    JobDetailsDto job = jobMonitoringApi.JobDetails(jobId);
    string resultSerialized = job.History[0].Data["Result"];
    List<TReturn> returnedItems = JsonConvert.DeserializeObject<List<TReturn>>(resultSerialized);
    return returnedItems;
}

Upvotes: 6

Pieter Alberts
Pieter Alberts

Reputation: 879

The "Result" field displays the output/result your method returns.

You can return a string or object and Hangfire will convert it to JSON.

Your "Result" field contains a serialization error, Hangfire is failing to serialize the returned object but without any code I cannot help you further.

Please see example screenshots below.

Code: Result: Code: Result:

Upvotes: 14

Related Questions