C# Mongo FirstOrDefaultAsync hangs

前端 未结 1 1266
情深已故
情深已故 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, since the result of FirstOrDefaultAsync() is the same type as the result you want to return.

      public Task GetFirst(FilterDefinition query)
      {
          return GetCollection.Find(query).FirstOrDefaultAsync();
      }
      

    0 讨论(0)
提交回复
热议问题