C# Mongo FirstOrDefaultAsync hangs

前端 未结 1 1268
情深已故
情深已故 2021-01-06 08:55

using the 2.0 driver the following code will sometimes hang and never return.

public async Task GetFirst(FilterDefinition query)
{
    retu         


        
相关标签:
1条回答
  • 2021-01-06 09:28

    There are 2 solutions:

    1. Add a ConfigureAwait(false) at the end:

      return await GetCollection.Find(query).FirstOrDefaultAsync().ConfigureAwait(false);
      
    2. Just return the Task<T>, since the result of FirstOrDefaultAsync() is the same type as the result you want to return.

      public Task<T> GetFirst(FilterDefinition<T> query)
      {
          return GetCollection.Find(query).FirstOrDefaultAsync();
      }
      
    0 讨论(0)
提交回复
热议问题