ToListAsync () does not complete at all

I want to do some async requests. But when I debug the code, in most cases, when ToListAsync () is called, the program stops. There is no visible exception and the callstack window is cleared. When I click on the pause button in VS, I see the stack frame just before the current method is called.

var res1 = await context.Address().Where(...).ToListAsync();
var res2 = await context.Person().Where(...).ToListAsync();
var res3 = await context.Rule().Where(...).ToListAsync();

      

Sometimes the first call works, in rare cases the second. But at least on the third call, the program stops. I do not know why...

+3


source to share


1 answer


From comments:

This is a wpf application. The commands are in an asynchronous method. I am calling this from a non-asynchronous method usingvar result = LoadAsync.Result();

Right there, it's a dead end . DispatcherSynchronizationContext

trying to merge the continuation (everything after the first await

) back into the dispatcher (UI) thread that is currently blocked on the callLoadAsync.Result



Decision:

  • await

    at the top of the call stack, forcing your method to return async Task

    instead Task.Result

    :

    await LoadAsync();
    
          

  • If you absolutely cannot change the method at the top of the stack to async Task

    , and for some obscure reason still needs to be called Task.Result

    , use ConfigureAwait(false)

    when you fix your internal async methods. This would mean that the sync context is not explicitly trying to redirect work to the dispatcher thread:

    var res1 = await context.Address().Where(...).ToListAsync().ConfigureAwait(false);
    var res2 = await context.Person().Where(...).ToListAsync().ConfigureAwait(false);
    var res3 = await context.Rule().Where(...).ToListAsync().ConfigureAwait(false);        
    
          

+11


source







All Articles