I have an asynchronous method in which I wish to invoke the SendAsync
method as a fire-and-forget service. Consider the following code:
public async Task HandleAsync(DoStuffCommand command)
{
/*
* Performing various tasks..
*/
_client.SendAsync(myObject);
}
public async Task SendAsync(MyObject myObject)
{
/*
* Time consuming tasks..
*/
try
{
await call_1();
Trace.TraceInformation("Call1");
await call_2();
Trace.TraceInformation("Call2");
}
catch (Exception e)
{
Trace.TraceError(e.Message);
throw;
}
}
My problem is that for some reason call_2 is inconsistently getting called (very rarely). My suspicion is that the SendAsync method is not allowed to complete because the calling method HandleAsync
does not await SendAsync
and when the HandleAsync
thread is terminated, the work in progress in SendAsync
is too.
But, this is contrary to my understanding of async/await. I was under the impression that the SendAsync
implementation would perform its work on a different thread in this scenario, and thus, be able to complete even if HandleAsync
would return before SendAsync
.
Maybe someone more async/await than myself can shed some light? Thank you.
UPDATE
I also tried adding a try/catch and traces. No exceptions are thrown but the trace consistently follows the behavior of the method calls, i.e. When both calls are made, so are both TraceInformation
and when only call_1
is invoked, only the first TraceInformation
runs.