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

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

问题描述

我正在测试新的MongoDB.Driver(适用于与dotnet核心兼容的c#)的beta版本,但无法正常工作.我有一个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(内部版本: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天全站免登陆