gpt4 book ai didi

c# - : MongoDB. Driver.MongoConnectionException:打开与服务器的连接时发生异常

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:34 36 4
gpt4 key购买 nike

因此,我在 Azure VM 上使用 MongoDB,并且我有一个网站作为服务托管在 Azure 网站上。我的问题是:有时我会收到这样的错误:

"Exception: MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"

出现此错误后,只需再次访问端点即可立即成功。

详细信息:

ConnectionString: mongodb://xxx.aaa.net:1000, xxx.aaa.net:1001, xxx.aaa.net:1002/?readPreference=nearest

在打开与 Mongo 的连接之前,我设置了 MaxConnectioIdleTimeout 属性,如下所示“MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromSeconds(30);” , 为了保持事件连接,因为 azure LoadBalancer 的空闲超时默认在 4 分钟后变为不活动状态。

所以,我不知道发生了什么。

有人可以帮我吗?

最佳答案

是的,4 分钟。如果连接 session 持续空闲长达 4 分钟,Azure 将关闭该连接 session 。但是 mongodb 驱动程序不知道这一点,它仍然会从连接池中获取该连接。您可以将 MaxConnectionIdleTime(在 mongodb 驱动程序设置中)设置为小于 4 分钟,或者通过 powershell 将 azure VM 的最大 session 空闲时间设置得更大(最长 30 分钟)。

顺便说一句,如果您的网站也托管在azure上,使用私有(private)IP(PIP)访问mongodb服务器,则不会出现此问题。

关于c# - : MongoDB. Driver.MongoConnectionException:打开与服务器的连接时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385566/

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