2015-10-04 29 views
0

我在C#上使用MongoDB並試圖做簡單的查詢。像線條 程序執行終止:C#+ MongoDB:程序終止並拋出沒有例外

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

或上線

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

它trows沒有例外,它具有Console.WriteLine( 「測試」)和到Console.ReadLine() 末該程序沒有執行。在cmd中,我看到與DB的連接已建立。

任何想法?

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(); 

更新。這條線:

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

給出了相同的結果 - 程序終止

+0

你怎麼能告訴它終止?它能在等待什麼嗎? –

+0

如果它沒有拋出異常,你如何判斷它終止?例如,你可能會遇到死鎖。 –

+1

控制檯在不執行Console.WriteLine(「test」)和Console.ReadLine()的情況下關閉,我將try-catch放到了每個地方,而且我還用槽來處理每一行,並且它從來沒有進入任何catch塊,並且studio的輸出表示線程0x26bc已退出,代碼爲0(0x0)。 –

回答

0

我也有類似的問題。這是對我工作:

 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); 
      } 
     } 
0

這聽起來像使用不當asyncawait。請注意,await實際上並未強制程序等待結果(這是通過Task.Wait()完成的)。 await只是指定了異步任務完成後應該完成的操作。如果您的程序包含對異步方法的調用,並且您永遠不會等待結果,它將在異步任務完成之前終止。