Kestrel .NET API-无法与mongodb // url连接

时间:2019-01-25 13:21:34

标签: .net mongodb azure service kestrel

我已经配置了连接到Mongo数据库的.net核心API。我已经从ConnectionString连接到它,例如“ mongodb // user:password @ server ...”。目前,在本地连接良好,我可以与MongoDB连接。问题是当我发布它时,服务器日志流向我显示了与Kestrel代理相关的内容,并且无法解析“ mongodb://”。实际上,mongodb字符串看起来像是未知的。

我已经在app.settings.json文件中配置了连接:

“ ConnectionStrings”:{     “ 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 ---

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。造成该错误的原因是该应用无法连接到MongoDB。 MongoDB数据库位于无法访问的网络中