0

Using an async Find call in a WCF application (C# MongoDB Driver v2.2.2.10) causes timeouts from time to time.

This is what we use today:

  public static async Task<T> FindOne<T>(this IMongoCollection<T> collection, FilterDefinition<T> filter = null, FindOptions options = null,
            CancellationToken token = default(CancellationToken)) where T : class
        {
            return await collection.Find(filter, options).FirstOrDefaultAsync(token);
        }

However, this tends to hang and this answer says I should add ConfigureAwait(false):

return await collection.Find(filter, options).FirstOrDefaultAsync(token).ConfigureAwait(false);

My question is, in a WCF context, is using ConfigureAwait(false) safe in an application that is heavily multi threaded?

Community
  • 1
  • 1
Nir
  • 3,963
  • 8
  • 37
  • 51
  • Where does it timeout? Driver? or even your code. Since it's multi-threaded app, it could be anywhere. I'd suggest to revisit what are you doing carefully and take advantage of profiling. – Saleem Mar 14 '16 at 12:54
  • Saleem, thank you, this is indeed all over the place, I am trying to see first if there's an error in the way we invoke this call. – Nir Mar 14 '16 at 15:56
  • 1
    Oh I see, you are using `collection.Find` which is blocking call. Replace it with `FindAsync` – Saleem Mar 14 '16 at 16:05
  • 1
    See https://docs.mongodb.org/getting-started/csharp/query/#query-for-all-documents-in-a-collection – Saleem Mar 14 '16 at 16:06
  • @Saleem this is incorrect, they are both Async, one returns all the documents and one (FindAsync) returns a cursor – Nir Mar 15 '16 at 11:09
  • This is the point. All vs cursor. Think if you have thousands of documents what will happen. – Saleem Mar 15 '16 at 11:15
  • @Saleem #facepalm thanks for the wake up call – Nir Mar 17 '16 at 14:44
  • 1
    You are welcome, It's community of helping hands and glad it worked for you. so no #facepalm but #smile. – Saleem Mar 17 '16 at 14:57

0 Answers0