Search code examples
c#mongodb

Issue connecting to MongoDB in release mode C#


I recently setup a MongoDB Collection for the first time. I've created some code that accesses a MongoDB user. Below is the Json file for a good idea of what I'm working with:

{
  "_id": {
    "$oid": "6***********************8"
  },
  "Username": "J",
  "Password": "k"
}

In my C# WinUI application (using Visual Studio 2022), I have code that accesses this data. I used the builtin nuget installer to install MongoDB.Driver V3.1.0 and I have using MongoDB.Driver; declared at the begining of each code file (haven't taken the time to implement global usings). Below is the code I'm using:

using System.Collections.Generic;
using Microsoft.UI.Xaml;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

namespace LearnMongoDBTest
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        private List<Template> templates;

        public MainWindow()
        {
            this.InitializeComponent();
            LoadDB();
        }

        public async void LoadDB()
        {
            var mongoClient = new MongoClient("<MyConnectionString>");
            var database = mongoClient.GetDatabase("CPSUSERS");
            var collection = database.GetCollection<Template>("CPSCollection");

            templates = await collection.Find(new BsonDocument()).Limit(10).ToListAsync();

            LoadLists();
        }

        public void LoadLists()
        {
            foreach (var entry in templates)
            {
                myListBox.Items.Add(entry.Username);
                myListBox2.Items.Add(entry.Password);
            }
        }
    }


    public class Template
    {
        [BsonId]
        public ObjectId Id { get; set; }

        [BsonElement("Username")]
        public string Username { get; set; }

        [BsonElement("Password")]
        public string Password { get; set; }
    }
}

All of that code works perfectly fine in Debug mode. It prints all users to the listboxes. However, when I switch the mode to release, I get a ton of errors in the output log (Exception thrown: 'MongoDB.Driver.MongoConnectionException' in MongoDB.Driver.dll, Exception thrown: 'System.MissingMethodException' in MongoDB.Bson.dll, Exception thrown: 'System.MissingMethodException' in MongoDB.Driver.dll) and I am unable to access the database. I'm quite new to MongoDB so any help is appreciated.


Solution

  • In the .csproj file of your WinUI 3 application, add:

    <PropertyGroup>
       <PublishTrimmed>false</PublishTrimmed>
    </PropertyGroup>
    

    Open your project in VS 2022 and right click project in solution explorer: Properties > Build > Publish > Uncheck Publish trimmed.


    The trimming optimization feature of VS 2022 (which only runs in release mode) trims necessary methods in the MongoDB.Driver dll file which causes the connection to the database to fail.