1

I am using MongoDB on C# and trying to do simple query. Program execution terminates on lines like:

var people = await collection.Find(filter).ToListAsync();

or on line

using (var cursor = await collection.FindAsync(filter))

It trows no exceptions, it has Console.WriteLine("test") and Console.ReadLine() at the end of the program which are not executed. In cmd I see that connection to DB is established.

Any ideas?

P.S.

 var filter = Builders<Follower>.Filter.Eq("id", f.id);
        List<Follower> fetchedFollowers = new List<Follower>();
        Console.WriteLine("0");
        try
        {
            using (var cursor = await collection.FindAsync(filter))
            {
                Console.WriteLine("1");
                while (await cursor.MoveNextAsync())
                {
                    var batch = cursor.Current;
                    foreach (Follower foll in batch)
                        fetchedFollowers.Add(foll);
                }
            }

        }
        catch (Exception e)
        {

            Console.WriteLine("Exception block");
            Console.WriteLine("2");
        }

        Console.WriteLine("3");
        Console.ReadLine();

Update. This line:

 var count = await collection.Find(filter).CountAsync();

gives the same result - program terminates

Anna F
  • 41
  • 6
  • How can you tell its terminating? Could it be waiting for something? – D. Ben Knoble Oct 04 '15 at 16:24
  • If it doesn't throw an exception, how do you figure it terminates? You could be hitting a deadlock for example. – Alexander Obersht Oct 04 '15 at 16:24
  • 1
    The console closes without executingConsole.WriteLine("test") and Console.ReadLine(), I put try-catch everywhere, and I also went trough each line with breakpoints and it never entered any catch blocks, and studio's output says The thread 0x26bc has exited with code 0 (0x0). – Anna F Oct 04 '15 at 16:46
  • 3
    Can you show the full code where you put a try and catch around the line that you think causes the program to terminate? and also include the `Console.Writeline` statements – Yacoub Massad Oct 04 '15 at 16:52
  • @BenKnoble The console closes without executingConsole.WriteLine("test") and Console.ReadLine(), I put try-catch everywhere, and I also went trough each line with breakpoints and it never entered any catch blocks, and studio's output says The thread 0x26bc has exited with code 0 (0x0). – Anna F Oct 04 '15 at 16:54
  • 1
    Chances are that mongo related code runs in its own thread so you won't catch it. You might try http://stackoverflow.com/questions/5762526/how-can-i-make-something-that-catches-all-unhandled-exceptions-in-a-winforms-a to handle uncaught exceptions - if program quits with an exception. You can also try ( in debugger) to stop on all exceptions being thrown (configuration option in Visual Studio). – Andreas Reiff Oct 04 '15 at 17:03
  • @YacoubMassad I added code in the main post – Anna F Oct 04 '15 at 17:10
  • @user3669608, so what you are saying is that you get "0" on the console, but then the program terminates before you get "1" or "Exception block" on the console, right? – Yacoub Massad Oct 04 '15 at 17:32
  • Is there a synchronous version of the method your are trying to invoke? And if there is, do you get the same issue if you use it? – Yacoub Massad Oct 04 '15 at 17:34
  • @YacoubMassad Yes, exactly. – Anna F Oct 04 '15 at 17:48
  • @YacoubMassad I am new to it and I haven't found synchonous method, but I was asking it here and it seems that im new mongo c# driver there are not – Anna F Oct 04 '15 at 17:49
  • @user3669608, I think there is a synchronous version called [Find](http://api.mongodb.org/csharp/current/html/Overload_MongoDB_Driver_IMongoCollectionExtensions_Find.htm?_ga=1.100249176.756406971.1443981016). Please try it to see if it causes the same issue or if it throws a meaningfull exception. – Yacoub Massad Oct 04 '15 at 17:55
  • @YacoubMassad Thanks. there is. can you help me please to move further? After I use people = collection.Find(filter); I need to make "people" variable enumerable (as I see from errors in studio) to make it possible to go thrhrough it woth "foreach". How can I do it? Or, I only need to check if query has returned 0 values or not. Is there easy way after using find()? – Anna F Oct 04 '15 at 18:05
  • I was using var people = await collection.Find(filter).ToListAsync(); before which had the same result. can I remove await from here? I actually dont need it – Anna F Oct 04 '15 at 18:06
  • all I actually need is for this line to work: var count = await collection.Find(filter).CountAsync(); and result is the same – Anna F Oct 04 '15 at 18:18
  • @AnnaF can you show how you call that code from main? – i3arnon Oct 05 '15 at 14:08
  • Opened a new question: http://stackoverflow.com/questions/32966361/mongodb-c-sharp-2-0-1-driver-connection-suddenly-closes – Craig Wilson Oct 06 '15 at 11:17

2 Answers2

3

It sounds like improper use of async and await. Note that await does not actually force the program to wait for the result (that is done with Task.Wait()). await just specifies what should be done when the asynchronous task completes. If your program consists of a call to an asynchronous method and you never wait for the result, it will terminate before the asynchronous task completes.

J. Lenthe
  • 1,330
  • 10
  • 10
0

I had a similar problem. This is what worked for me:

        using (var cursor = collection.FindAsync(filter))
        {
            Console.WriteLine("1");
            while (cursor.Result.MoveNext())
            {
                var batch = cursor.Result.Current;
                foreach (Follower foll in batch)
                    fetchedFollowers.Add(foll);
            }
        }
Kevin M
  • 41
  • 1