await for some reason does not seem to resume on the calling context. I'm calling an async method (actually a hierarchy of several async methods). Things go fine till I finally reaches the DataService which uses RestSharp's ExecuteAsync(). That final call looks like this:
IRestResponse Response = await rc.ExecuteAsync(request); //rc is a RestClient object
I have debugged it thoroughly and at each level, the following line returns true, even just before calling ExecuteAsync:
System.Threading.Thread.CurrentThread == System.Windows.Application.Current.Dispatcher.Thread
Only after the ExecuteAsync() returns with server data, the above line becomes false, indicating that the above await did not resume on the calling thread. Thereupon, the higher layers that called this async operation and now want to assign returned data to the UI throw the infamous STA thread exception.
What am I doing wrong? I have burnt several hours already and gone through several SO posts and web articles, but this doesn't seem to work. I have also added ConfigureAwait(true) to explicitly ask it to resume on calling context, but it didn't.
This is a VSTO Word add-in. I'm using it with WPF.