Why using (var cursor = await collection.FindAsync(filter)) is not working?

661 views
Skip to first unread message

Prashant Maurya

unread,
Oct 22, 2017, 5:38:51 PM10/22/17
to mongodb-dev
I have tried my level best to access my db using the methods specified in the mongoDb official.
                     using (var cursor = await collection.FindAsync(filter))                    
                                                    {
                while (await cursor.MoveNextAsync())
                {
                    var batch = cursor.Current;
                    foreach (var document in batch)
                    {

                        user.Add(new
                        {
                            UserId = document["_id"].ToString(),
                            FName = document["FName"].ToString(),
                            LName = document["LName"].ToString(),
                            ActiveStatus = document["ActiveStatus"].ToBoolean()

                        });



                    }
                }
            }
But the line var cursor=await collection.FindAsync(filter) is not executing.
Interesting part is the same piece of code working in console application. The application which i am working currently is MVC Application using .Net Framework.
Please tell me the solution to solve the above mentioned query.
I have posted the same question on stackoverflow also.

Robert Stam

unread,
Oct 23, 2017, 11:08:17 AM10/23/17
to mongo...@googlegroups.com
Your code looks correct.

One possibility is that you are experiencing an async deadlock. Read up on ConfigureAwait(false) and try changing your await calls to:

await collection.FindAsync(filter).ConfigureAwait(false)
await cursor.MoveNextAsync().ConfigureAwait(false)


--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev+unsubscribe@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-dev/9b8d9978-1ebd-4c74-8885-4ae592beacc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ivan Artemov

unread,
Oct 23, 2017, 1:47:27 PM10/23/17
to mongodb-dev
ConfigureAwait is mostly library-side method and use it in client code is bad practice, especially for this simple use case

понедельник, 23 октября 2017 г., 18:08:17 UTC+3 пользователь Robert Stam написал:
Your code looks correct.

One possibility is that you are experiencing an async deadlock. Read up on ConfigureAwait(false) and try changing your await calls to:

await collection.FindAsync(filter).ConfigureAwait(false)
await cursor.MoveNextAsync().ConfigureAwait(false)

On Sun, Oct 22, 2017 at 3:10 AM, Prashant Maurya <er.maury...@gmail.com> wrote:
I have tried my level best to access my db using the methods specified in the mongoDb official.
                     using (var cursor = await collection.FindAsync(filter))                    
                                                    {
                while (await cursor.MoveNextAsync())
                {
                    var batch = cursor.Current;
                    foreach (var document in batch)
                    {

                        user.Add(new
                        {
                            UserId = document["_id"].ToString(),
                            FName = document["FName"].ToString(),
                            LName = document["LName"].ToString(),
                            ActiveStatus = document["ActiveStatus"].ToBoolean()

                        });



                    }
                }
            }
But the line var cursor=await collection.FindAsync(filter) is not executing.
Interesting part is the same piece of code working in console application. The application which i am working currently is MVC Application using .Net Framework.
Please tell me the solution to solve the above mentioned query.
I have posted the same question on stackoverflow also.

--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.

Robert Stam

unread,
Oct 24, 2017, 9:45:14 AM10/24/17
to mongo...@googlegroups.com
I understand. I thought it was worth suggesting since you say it works in a console application.

You could also try using the synchronous API if you aren't committed to using async.

To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev+unsubscribe@googlegroups.com.

To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
Reply all
Reply to author
Forward
0 new messages