0
votes

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?

1
the connection string seems to be empty in MongoClient, See this example here - Kunal Mukherjee
a connection string didnt help me MongoClient client = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0"); - Costea Voda

1 Answers

0
votes

I try to make it async and it works.

        private void button1_Click(object sender, EventArgs e)
        {
            async void a()
            {
                MongoClient client = new MongoClient();
                var db = client.GetDatabase("test1");
                IMongoCollection<Person> collection = db.GetCollection<Person>("people");
                var people = await collection.Find(new BsonDocument()).ToListAsync();
                foreach (Person person in people)
                {
                    MessageBox.Show($"{person.FirstName} => {person.LastName}");
                }
            }
            a();
        }

Is there a way to make it works sync?