Async method never returns for simple c# call.

187 views
Skip to first unread message

pbu...@compuserve.com

unread,
Jun 10, 2015, 8:40:59 PM6/10/15
to mongod...@googlegroups.com
I must be doing something very simply wrong.  I have mongodb version 3.0.3 with the latest drivers.  I have code like this
        async Task FooAsync()
        {
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("PSB01");
            var collection = database.GetCollection<Book>("book");

            await collection.InsertOneAsync(new Book { name = "Get there please" });

            // next statements....
        }

In my 'main' routine I call FooAsync().Wait();

It never seems to get to the next statement and never returns. 
BUT it turns out the book really was inserted - I see it when I list it, so the insert worked yet the task never returned.

I am using VS2012 with.Net 4.5.1 - could that be it?

Any thought appreciated.  Driving me nutz.  Thank you.

Craig Wilson

unread,
Jun 10, 2015, 9:38:12 PM6/10/15
to mongod...@googlegroups.com
You are probably deadlocking. In all places where you are doing an await, add a ConfigureAwait(false) to the end:

await collection.InsertOneAsync(new Book { name = "Get there please" }).ConfigureAwait(false);

Craig

pbu...@compuserve.com

unread,
Jun 11, 2015, 11:31:39 AM6/11/15
to mongod...@googlegroups.com
Absolutely fixed it.  Thanks Craig.  Appreciate your time very much.
Reply all
Reply to author
Forward
0 new messages