Hi,
I have a requirement to do some time consuming operation(to create a html template based on the request) in Web Api and convert it as a pdf and send as attachment in mail.This has to happen as a background job.
The problem is I have a query handler which is an async method and this method is used to create multiple templates and is very much time consuming and we don't want the user to wait till we complete this operation. So we have decided to use HangFire.
But the problem here is the job is not executing and in most of the forums they have mentioned that only synchronous calls can be done using HangFire.
So I wanted to know whether is it possible to call the asynchronous method synchronously, so that I will call this synchronous method inside backgroundJob.Enqueue().If so can you please help me with some example.
Note: I am doing further operations based on the result of that async method
What I have tried:
I tried calling the method asynchronous method synchronously using
Task.Run(()=>AsyncMethod()).Result
and also tried
Task.Run(async()=>await AsyncMethod()).Result, Task.Run(()=>AsyncMethod()).ConfigureAwait(false).GetAwaiter().GetResult()
like,
public void HangFireMethodCall(){
BackgroundJob.Enqueue(()=>SyncMethod());
}
public SomeReturnType SyncMethod(){
Task.Run(async()=>await AsyncMethod(Request)).Result;
return successMessage;
}
public async Task<returntype> AsyncMethod(RequestType request){
var response= await ExecuteAsync(request);
}
But nothing executed the async method.
few referral links I have tried are,
Async task jobs - question - Hangfire Discussion[
^]
async await - How to call asynchronous method from synchronous method in C#? - Stack Overflow[
^]