Mongodb C# 'System.TimeoutException

2019-08-27 06:35发布

I got strange exception in mongodb.I wrote a simple service into vs and worked well till last night. but now when I am running my service by VS I get this exception:

An exception of type 'System.TimeoutException' occurred in MongoDB.Driver.Core.dll but was not handled in user code

Additional information: A timeout occured after 30000ms selecting a  server using CompositeServerSelector{ Selectors =  ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = []  } }, 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 : "127.0.0.1:27017" }", EndPoint:  "127.0.0.1:27017", State: "Disconnected", Type: "Unknown",  HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception  occurred while opening a connection to the server. --->  System.Net.Sockets.SocketException: No connection could be made because the  target machine actively refused it 127.0.0.1:27017

at System.Net.Sockets.Socket.EndConnect(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 ---

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__47.MoveNext()

 --- End of inner exception stack trace ---

at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__47.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 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)

at MongoDB.Driver.Core.Servers.ClusterableServer.<HeartbeatAsync>d__42.MoveNext()" }] }.

why I got this exception and how could I handle it? why my service worked well before? this is where I got this exception:

        public static IList<Location> GetFinalLocationsInTopicInLocationTable(string topic)
    {
        logger.Trace(Constants.LOG_MESSAGE_METHOD_STARTED);

        var collection = GetDbMongo().GetCollection<Location>(DB_COLLECTION_LOCATIONS);
        var matchFilter = Builders<Location>.Filter.Regex("Topic", topic);
        var groupFilter = new BsonDocument { { "_id", "$DeviceId" }, { "max", new BsonDocument("$max", "$CreationDateTime") } };

        var location = collection
            .Aggregate()
         .Match(matchFilter)
         .Group(groupFilter)
         .ToList();     // -----I got this exception

1条回答
混吃等死
2楼-- · 2019-08-27 06:42

I resolve that. my problem was: my mongodb service was stopped. I do not know why it was stopped but when I start it, the problem gone.

查看更多
登录 后发表回答