Error using backgroundjob.enqueue


#1

I am just getting started exploring hangfire and its use to schedule jobs in the background. I am working on a simple POC that would place a httpwebrequest on a queue if the service was not available to be called.

try{ attempt call}
catch(webexpcetion)
{ BackgroundJob.Enqueue(() => MakeCall(httpWebRequest));}

MakeCall method is in the same class as the try/catch. I have also tried it in a separate class and called it with BackgroundJob.Enqueue(x => x.MakeCall(httpWebRequest));

In either scenario I get the following error and I am not finding a solution:

Can not find the target method.
Failed
Can not change the state to ‘Processing’: target method was not found.
Newtonsoft.Json.JsonSerializationException
Error setting value to ‘Host’ on ‘System.Net.HttpWebRequest’.

On retry the only difference is trying to change the state to enqueue instead of processing.

Any help resolving or pointed in the right direction to look would be great!
Thanks


#2

Since the state/arguments for the job are json-serialized you need to make sure that it’s serializable. A HttpWebRequest probably isn’t. I suggest you create your own argument-object containing the data you want to use when executing the webrequest and create your webrequest-object in MakeCall-method.