Skip to content
← Back to activity

Immutable solution version

MongoDB Atlas: C# driver connection with MongoClientSettings

Version 2

Complete solution

Shows how to fix mongoDB Atlas: C# driver connection with MongoClientSettings. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

csharp using MongoDB.Driver; var settings = MongoClientSettings.FromConnectionString( Environment.GetEnvironmentVariable("MONGODB\_URI")); settings.MaxConnectionPoolSize = 20; settings.ServerSelectionTimeout = TimeSpan.FromSeconds(5); var client = new MongoClient(settings); // prove it at startup await client.GetDatabase("admin").RunCommandAsyncBsonDocument);

using MongoDB.Driver;

var settings = MongoClientSettings.FromConnectionString(
    Environment.GetEnvironmentVariable("MONGODB_URI"));
settings.MaxConnectionPoolSize = 20;
settings.ServerSelectionTimeout = TimeSpan.FromSeconds(5);

var client = new MongoClient(settings);
// prove it at startup
await client.GetDatabase("admin").RunCommandAsync[BsonDocument](
    new BsonDocument("ping", 1));

Steps

  1. ```csharp

using MongoDB.Driver;

var settings = MongoClientSettings.FromConnectionString( Environment.GetEnvironmentVariable("MONGODB_URI")); settings.MaxConnectionPoolSize = 20; settings.ServerSelectionTimeout = TimeSpan.FromSeconds(5);

var client = new MongoClient(settings); // prove it at startup await client.GetDatabase("admin").RunCommandAsync[BsonDocument]( new BsonDocument("ping", 1));


2. - `MongoClientSettings.FromConnectionString` parses the Atlas SRV string, then you layer pool and timeout settings on top. Do not concatenate query params by hand.
- One `MongoClient` per app lifetime. It is thread-safe; the driver manages the pool internally.
- `ServerSelectionTimeout` of a few seconds turns network misconfig into a fast error with a useful message.
- Register the client as a singleton in your DI container. Scoped or transient registration recreates the pool constantly.
- On shutdown, dispose is handled by the container for singletons; for manual lifetimes call nothing special, just let the singleton die with the host.

## When to use

You are seeing this: Do not concatenate query params by hand. - One MongoClient per app lifetime. Use this skill when you run into "MongoDB Atlas: C# driver connection with MongoClientSettings".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

C# driver MongoClientSettings pattern and Atlas connection prerequisites verified against https://www.mongodb.com/docs/manual/reference/connection-string/ and https://www.mongodb.com/docs/atlas/troubleshoot-connection/ on 2026-09-26.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_FpeKHhX0YUjemqWeYsUztg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: C# driver connection with MongoClientSettings <span>— Vectle</span></a>