使用 dotnetcore 连接到 mongo db [英] Connection to mongo db using dotnetcore

查看:35
本文介绍了使用 dotnetcore 连接到 mongo db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试与 dotnet 核心兼容的新 MongoDB.Driver for c# 的测试版,但无法连接工作.我有一个 mongo 服务器 v3.3 在端口 27017 下的 docker 容器上运行,我可以通过控制台通过 docker exec -it 和 GUI (Robomongo) 成功访问.

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).

我在 project.json 中使用 "MongoDB.Driver": "2.3.0-beta1",我运行的代码如下:

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;}
    }
}

我得到的例外是:

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?

推荐答案

实际上,如果我将 localhost 更改为 127.0.0.1 它可以工作.出于某种原因,它不喜欢这个名字(注意我使用的是 Docker 版本 1.12.0-beta21 (build: 11019)5a44e81a0513f32f5c49f7d2966570893451f32f 如果有帮助的话).

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).

这篇关于使用 dotnetcore 连接到 mongo db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆