EntityFramework ToListAsync() does not work

.net async-await c# entity-framework-6

Question

I try to call EF method ToListAsync. But nothing happend - no exception, no timeout just running.

This is my code.

        private IQueryable<Place> placeCompleteQuery;
    protected IQueryable<Place> PlaceCompleteQuery
    {
        get
        {
            return this.placeCompleteQuery ?? (this.placeCompleteQuery = this.Context.Places.Include(p => p.Address).
                Include(p => p.CreatedBy).
                Include(p => p.Source).
                Include(p => p.Type.Translations).
                Include(p => p.Ratings));
        }
    }

    public async Task<IList<Place>> GetPlacesByLocationAsync(DbGeography location, int radius)
    {
        List<Place> temporaryResult = PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius).
            ToList();

        return await PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius).
            ToListAsync();
    }

The first sync call of ToList method return result immediately. The second async call of ToListAsync still running with no result nor exception.

Any suggestions?

1
8
2/25/2014 2:42:39 PM

Accepted Answer

I suspect that further up your call stack, your code is calling Task.Wait or Task<T>.Result. If you do this on the UI thread or from an ASP.NET request context, your code will deadlock, as I explain on my blog.

To fix it, use await instead of Task.Wait or Task<T>.Result.

19
2/25/2014 2:51:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow