I'm testing the beta of the new MongoDB.Driver for c# compatible with dotnet core and I can't get the connection to work. I have a mongo server v3.3 running on a docker container under port 27017 and I can sucessfully access through either the console via docker exec -it
and a GUI (Robomongo).
I am using the "MongoDB.Driver": "2.3.0-beta1"
, in my project.json and the code I run is the following:
public class Program
{
public static void Main(string[] args)
{
var client = new MongoClient("mongodb://localhost:27017");
var db = client.GetDatabase("newdatabase");
var collection = db.GetCollection<Person>("mycollection");
collection.InsertOne(new Person{Id = "1", Age = 32});
}
public class Person
{
public string Id{ get; set;}
public int Age{ get; set;}
}
}
The exception I get back is:
A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.PlatformNotSupportedException: This platform does not support connecting sockets to DNS endpoints via the instance Connect and ConnectAsync methods, due to the potential for a host name to map to multiple IP addresses and sockets becoming invalid for use after a failed connect attempt. Use the static ConnectAsync method, or provide to the instance methods the specific IPAddress desired.
at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
at System.Net.Sockets.Socket.Connect(String host, Int32 port)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.<ConnectAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.<CreateStreamAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
--- End of inner exception stack trace ---
at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Servers.ServerMonitor.<HeartbeatAsync>d__27.MoveNext()" }] }.
Is there something obvious that I am missing or misconfiguring or is it early days for this driver to be used?
Actually if I change localhost to 127.0.0.1 it works. For some reason it doesn't like the name (note I'm using Docker Version 1.12.0-beta21 (build: 11019) 5a44e81a0513f32f5c49f7d2966570893451f32f if that's of any help).
Had the same problem running the dotnet core application in Kubernetes cluster hosted in Docker-Desktop on Windows. I've tried Debian and Alpine based images and had no luck.
Also it doesn't seem to be a Docker issue, since any nslookup hostname
from the pod resolves the correct IP address.
The only workaround I found is to resolve the hostnames to IP addresses before initializing the Mongo connection, so I end up with the utility method:
private string GetMongoConnectionStringWithIps(string connectionString) {
var builder = new MongoUrlBuilder(connectionString);
var servers = new List<MongoServerAddress>();
foreach (var server in builder.Servers)
{
var address = Dns.GetHostAddresses(server.Host).FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
servers.Add(new MongoServerAddress(address.ToString(), server.Port));
}
builder.Servers = servers;
return builder.ToMongoUrl().ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With