I am new to docker and mongoDB, am using docker toolbox on widows 10 home machine.while I tried to follow a tutorial on medium on docker, .net core and mongodb am getting the following timeout error
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 : "xxx.xxx.xx.xxx:27017" }", EndPoint: "xxx.xxx.xx.xxx:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xxx.xx.xxx:27017
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.<>c.b__271_0(IAsyncResult iar) --- End of stack trace from previous location where exception was thrown --- at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)" }] }.
Configuration Class
namespace TodoApp.Common.Config
{
public class MongoDbConfig
{
public string Database { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string User { get; set; }
public string Password { get; set; }
public string ConnectionString
{
get
{
if (string.IsNullOrWhiteSpace(User) || string.IsNullOrWhiteSpace(Password))
{
return $@"mongodb://{Host}:{Port}";
}
return $@"mongodb://{User}:{Password}@{Host}:{Port}/{Database}?connect=replicaSet";
}
}
}
}
Context class
namespace TodoApp.Common.Models
{
public class TodoContext : ITodoContext
{
private readonly IMongoDatabase _db;
public TodoContext(IOptions<MongoDbConfig> config)
{
var client = new MongoClient(config.Value.ConnectionString);
_db = client.GetDatabase(config.Value.Database);
}
public IMongoCollection<Todo> Todos => _db.GetCollection<Todo>("Todos");
}
}
appsettings
"MongoDB": {
"Database": "TodoDB",
"Host": "xxx.xxx.xx.xxx",
"Port": "27017",
"User": "root",
"Password": "example"
},
docker-compose.yml
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
Medium link : this