EntityFramework ToListAsync()不起作用

Mar*_*roš 8 .net c# async-await entity-framework-6

我尝试调用EF方法ToListAsync.但没有任何事情发生 - 没有例外,没有超时只是运行.

这是我的代码.

        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();
    }
Run Code Online (Sandbox Code Playgroud)

ToList方法的第一次同步调用立即返回结果.ToListAsync的第二次异步调用仍在运行,没有结果也没有异常.

有什么建议?

Ste*_*ary 19

我怀疑你的调用堆栈更进一步,你的代码正在调用Task.WaitTask<T>.Result.如果你在UI线程或ASP.NET请求上下文中执行此操作,您的代码将会死锁,正如我在我的博客上解释的那样.

要修复它,请使用await而不是Task.WaitTask<T>.Result.