Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework ToListAsync() does not work

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?

like image 425
Martin Majoroš Avatar asked Feb 25 '14 14:02

Martin Majoroš


1 Answers

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.

like image 55
Stephen Cleary Avatar answered Sep 28 '22 08:09

Stephen Cleary