I need to run task with current SynchronizationContext. I do it using Task factory:
Task.Factory.StartNew(async () =>
{
...
var result =
await service.AsyncOperation(data);
...
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
AsyncOperation method:
object async AsyncOperation(object data){
...
//some async operation here
await dal.SaveToDb(data);
...
}
The problem:
after executing await dal.SaveToDb(data);
AsyncOperation method is not being resumed (code after dal.SaveToDb(data) is not executed at all). Seems after await it releases thread to thread pool but after async operation complete (SaveToDb) does not resume execution of remaining code.
Why is it happening ??? How can I fix it if I do really need to run task with current SynchronizationContext (because I need HttpContext there)