我已经配置了连接到Mongo DB的.net core API。我已经从ConnectionString连接到它,如"mongodb//user:password@server..."。目前,在本地连接很好,我可以与MongoDB连接。问题是当我发布它时,服务器日志流向我显示与 Kestrel 代理相关的内容,并且无法解析"mongodb://"。事实上,mongodb 字符串看起来就像未知的。
我已经在app.settings.json文件中配置了连接:
"连接字符串":{ "db": "mongodb://admin:blbablabla@url:27017/?authSource=admin"
var uri = new MongoUr(_configuration.GetSection(CONNECTIONSTRINGS)[db];
_mongoDBClient = new MongoClient(uri);
这是错误:
2019-01-25 13:10:42.366 +00:00 [Error] Microsoft.AspNetCore.Server.Kestrel: Connection id "0HLK2OOVSKRD5", Request id "0HLK2OOVSKRD5:00000001": An unhandled exception was thrown by the application.
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 : "
Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId:
"{ ClusterId : 1, EndPoint : "Unspecified/urlmachine:27017" }", EndPoint:
"Unspecified/urlmachine:27017", State: "Disconnected", Type: "Unknown",
HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception
occurred while opening a connection to the server. --->
System.Net.Sockets.SocketException: No such host is known
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
at System.Net.Dns.<>c.<GetHostAddressesAsync>b__25_1(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
我已经解决了这个问题。导致该错误是因为应用程序无法连接到MongoDB。MongoDB DB位于无法访问的网络中