gpt4 book ai didi

mongodb - 使用 dotnet core 连接到 mongodb

转载 作者:可可西里 更新时间:2023-11-01 10:00:22 24 4
gpt4 key购买 nike

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

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

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()" }] }.

是否有明显的我遗漏或配置错误的东西,或者这个驱动程序的使用还处于早期阶段?

最佳答案

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

关于mongodb - 使用 dotnet core 连接到 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38772459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com