I have this code and it works fine for console:
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
MongoClient client = new MongoClient();
var db = client.GetDatabase("test1");
IMongoCollection<Person> collection = db.GetCollection<Person>("people");
List<Person> people = collection.Find(new BsonDocument()).ToList();
foreach (Person person in people)
{
//MessageBox.Show($"{person.FirstName} => {person.LastName}");
Console.WriteLine($"{person.FirstName} => {person.LastName}");
}
Console.ReadKey();
}
}
[BsonIgnoreExtraElements]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Now the same code for a simple wpf application.
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
namespace MongoWpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MongoClient client = new MongoClient();
var db = client.GetDatabase("test1");
IMongoCollection<Person> collection = db.GetCollection<Person>("people");
List<Person> people = collection.Find(new BsonDocument()).ToList();
foreach (Person person in people)
{
MessageBox.Show($"{person.FirstName} => {person.LastName}");
//Console.WriteLine($"{person.FirstName} => {person.LastName}");
}
}
}
[BsonIgnoreExtraElements]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Gives me System.TimeoutException
"A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", ReasonChanged: "ServerInitialDescription", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", LastHeartbeatTimestamp: null, LastUpdateTimestamp: "2020-08-23T15:42:56.2882193Z" }] }."
What I'm doing wrong?
MongoClient, See this examplehere- Kunal Mukherjee