Issue connecting asp.net to a mongodb replicaset

7/6/2019

I have developed a ASP.Net application locally on windows with a local mongodb database. So now I want to deploy it on my google kubernetes cluster and therefor I created a stateless mongodb replicaset. So far so good. I want to connect to that replicaset, but my app always gives me a connection refused error, but I can connect to that mongodb from another pod with ping.

So I changed my connection string from mongodb://mongo-0.mongo,mongo-1.mongo to mongodb://mongo-0.mongo,mongo-1.mongo?connect=replicaSet that didnt work. I tried scaling the replicaset etc, but that didnt work neither.

Thats how I connect to the database:

        public DatabaseContext(string dbUrl)
        {
            var client = new MongoClient(dbUrl);
            _database = client.GetDatabase("test");
        }

Here is the error:

Connection id "0HLO23AT5M5C3", Request id "0HLO23AT5M5C3:00000001": An unhandled exception was thrown by the application.
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 : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/mongo-0.mongo:27017" }", EndPoint: "Unspecified/mongo-0.mongo:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 10.4.0.5:27017

How would I connect to that replicaset?

-- Lukas Germerott
asp.net
asp.net-mvc-4
c#
google-kubernetes-engine
mongodb

0 Answers