【问题标题】:.Net core connection to mongo db not working.Net 核心连接到 mongo db 不起作用
【发布时间】:2022-02-22 21:56:59
【问题描述】:

我正在尝试使用 .net Core 连接到 Mongo DB,但是在调用 api 时出现超时错误。 我遵循了这个教程:https://medium.com/swlh/how-to-use-a-mongodb-database-in-a-asp-net-core-web-api-application-b0451ae314f5

您可以在这个 git repo 中查看本教程的完整代码:https://github.com/walpoles93/AspNetCoreMongoDb

我还尝试下载此存储库并添加我的 mongo 用户和密码,但仍然出现同样的错误。 我得到的错误是: System.TimeoutException: 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 : "Unspecified/cluster0-shard-00-00.tvnqj.mongodb.net:27017" }", EndPoint: "Unspecified/cluster0-shard-00-00.tvnqj.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.

以前有人遇到过这个问题吗?

【问题讨论】:

    标签: .net mongodb asp.net-core mongodb-query


    【解决方案1】:

    我认为您应该遵循有关使用 MongoDB 创建 .Net Core API 的官方 Microsoft 文档。

    Create a web API with ASP.NET Core and MongoDB

    希望我能帮上一点忙。

    【讨论】:

    • 试过了,但我正在尝试连接到云 mongo db 而不是本地。
    • 您是否尝试过阅读 MongoDB 关于 Atlas 的文档? docs.atlas.mongodb.com/connect-to-cluster
    • 我可以使用 nodejs 连接到同一个集群和数据库,但不能使用 .net core..
    • @אוריזבידה .net 有时可能有点难以设置。确保您的 .net 框架已更新。我也建议使用 sql 数据库与 .net 核心连接,因为它们更兼容
    【解决方案2】:

    我遇到了类似的问题,我设法解决的方法是将我的 IP 地址添加到 MongoDB 云仪表板上的数据库的允许网络连接列表中!

    【讨论】:

      【解决方案3】:

      这是一个使用 ASP.NET Core 和 Mongo 的 CRUD 操作示例。下面我附上链接

      你可以refer here

      它会帮助你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 2017-06-03
        • 2018-04-04
        相关资源
        最近更新 更多