Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to connect to the MongoDB server through a ssl certificate using c#. I am getting a System.TimeoutException (A timeout occurred after 30000ms selecting a server using the CompositeServerSelector).

What I have tried:

I started with connection via MongoClientSetting object. Here is the code:


MongoClientSettings settings = new MongoClientSettings();
settings.MaxConnectionLifeTime = new TimeSpan(12, 0, 0);

settings.UseSsl = true;
settings.VerifySslCertificate = false;
var cert = new X509Certificate2("mongoDBCAFile.cer");
settings.SslSettings = new SslSettings{
    ClientCertificates = new[] { cert }
};

settings.Servers = new[]{
    new MongoServerAddress("xyz1.intranet.companyname.com", 12345),
    new MongoServerAddress("xyz2.intranet.companyname.com", 12345)
};

settings.ReplicaSetName = "replicaName";

var cred = MongoCredential.CreateGssapiCredential("username@intranet.companyname.com").WithMechanismProperty("SERVICE_NAME", "servicename");
settings.Credential = cred;

var client = new MongoClient(settings);

var database = client.GetDatabase("DatabaseName");
var collection = database.GetCollection<BsonDocument>("CollectionName");

//This is the place of error
var count1 = collection.CountDocuments(new BsonDocument());


I tried playing around with ConnectTimeout, SocketTimeout, and wTimeOut but the error was same.

I also tried doing the same thing using the connection strinG but I wouldn't figure out how to create a connection string with these many parameters.
Posted
Updated 14-Sep-18 6:39am

1 solution

I haven't tried using mongoDB before, but typically a timeout suggest that a connection was being refused due incorrect configuration. Try to set the connectionString instead like this:

C#
var connectionString = @"mongodb://username%40REALM.com:password@myserver/?authMechanism=GSSAPI";
var client = new MongoClient(connectionString);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900