I have the following async
long running method inside my asp.net mvc-5 web application :-
public async Task<ScanResult> ScanAsync(string FQDN)
{
// sample of the operation i am doing
var c = await context.SingleOrDefaultAsync(a=>a.id == 1);
var list = await context.Employees.ToListAsync();
await context.SaveChangesAsync();
//etc..
}
and i am using Hangfire tool which support running background jobs to call this async method on timely basis, but un-fortuntly the hangefire tool does not support calling async methods directly . so to overcome this problem i created a sync version of the above method , as follow:-
public void Scan()
{
ScanAsync("test").Wait();
}
then from the HangFire scheduler i am calling the sync method as follow:-
RecurringJob.AddOrUpdate(() => ss.Scan(), Cron.Minutely);
so i know that using .Wait()
will mainly occupy the iis thread during the method execution ,, but as i mentioned i need to do it in this way as i can not directly call an async TASK inside the hangefire scheduler .
so what will happen when i use .Wait()
to call the async method ?, will the whole method's operations be done in a sync way ? for example as shown above i have three async operations inside the ScanAsync()
;SingleOrDefualtAsync
,ToListAsync
& SaveChangesAsync
, so will they be executed in sync way because i am calling the ScanAsync method using .wait() ?
RecurringJob.AddOrUpdate(async () => await ss.ScanAsync(), Cron.Minutely);
- Igorvar task = ScanAsync("test"); task.Wait(); if(task.Exception != null) {throw task.Texception};
If you don't do this, you may not find out if you had an exception in your background job. - mason