/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (!_disposed) { try { if (_cursorId != 0) { using (var source = new CancellationTokenSource(TimeSpan.FromSeconds(10))) { KillCursor(_cursorId, source.Token); } } } catch { // ignore exceptions } if (_channelSource != null) { _channelSource.Dispose(); } } } _disposed = true; }
/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (!_disposed) { CloseIfNotAlreadyClosedFromDispose(); if (_channelSource != null) { _channelSource.Dispose(); } _disposed = true; } } }
// constructors /// <summary> /// Initializes a new instance of the <see cref="AsyncCursor{TDocument}"/> class. /// </summary> /// <param name="channelSource">The channel source.</param> /// <param name="collectionNamespace">The collection namespace.</param> /// <param name="query">The query.</param> /// <param name="firstBatch">The first batch.</param> /// <param name="cursorId">The cursor identifier.</param> /// <param name="batchSize">The size of a batch.</param> /// <param name="limit">The limit.</param> /// <param name="serializer">The serializer.</param> /// <param name="messageEncoderSettings">The message encoder settings.</param> public AsyncCursor( IChannelSource channelSource, CollectionNamespace collectionNamespace, BsonDocument query, IReadOnlyList <TDocument> firstBatch, long cursorId, int batchSize, int limit, IBsonSerializer <TDocument> serializer, MessageEncoderSettings messageEncoderSettings) { _operationId = EventContext.OperationId; _channelSource = channelSource; _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); _query = Ensure.IsNotNull(query, nameof(query)); _firstBatch = Ensure.IsNotNull(firstBatch, nameof(firstBatch)); _cursorId = cursorId; _batchSize = Ensure.IsGreaterThanOrEqualToZero(batchSize, nameof(batchSize)); _limit = Ensure.IsGreaterThanOrEqualToZero(limit, nameof(limit)); _serializer = Ensure.IsNotNull(serializer, nameof(serializer)); _messageEncoderSettings = messageEncoderSettings; if (_limit == 0) { _limit = int.MaxValue; } if (_firstBatch.Count > _limit) { _firstBatch = _firstBatch.Take(_limit).ToList(); } _count = _firstBatch.Count; // if we aren't going to need the channel source we can go ahead and Dispose it now if (_cursorId == 0 && _channelSource != null) { _channelSource.Dispose(); _channelSource = null; } }