Class Watcher<T>
Watcher for subscribing to mongodb change streams.
public class Watcher<T> where T : IEntity
Type Parameters
TThe type of entity
- Inheritance
-
Watcher<T>
- Inherited Members
Properties
CanRestart
Returns true if watching can be restarted if it was stopped due to an error or invalidate event. Will always return false after cancellation is requested via the cancellation token.
public bool CanRestart { get; }
Property Value
IsInitialized
Indicates whether this watcher has already been initialized or not.
public bool IsInitialized { get; }
Property Value
Name
The name of this watcher instance
public string Name { get; }
Property Value
ResumeToken
The last resume token received from mongodb server. Can be used to resume watching with .StartWithToken() method.
public BsonDocument? ResumeToken { get; }
Property Value
- BsonDocument
Methods
ReStart(BsonDocument?)
If the watcher stopped due to an error or invalidate event, you can try to restart the watching again with this method.
public void ReStart(BsonDocument? resumeToken = null)
Parameters
resumeTokenBsonDocumentAn optional resume token to restart watching with
Start(EventType, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, bool, CancellationToken)
Starts the watcher instance with the supplied parameters
public void Start(EventType eventTypes, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool onlyGetIDs = false, bool autoResume = true, CancellationToken cancellation = default)
Parameters
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
filterFunc<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>b => b.Eq(d => d.FullDocument.Prop1, "value")
batchSizeintThe max number of entities to receive for a single event occurence
onlyGetIDsboolSet to true if you don't want the complete entity details. All properties except the ID will then be null.
autoResumeboolSet to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
Start(EventType, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, bool, CancellationToken)
Starts the watcher instance with the supplied parameters
public void Start(EventType eventTypes, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool onlyGetIDs = false, bool autoResume = true, CancellationToken cancellation = default)
Parameters
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
filterExpression<Func<ChangeStreamDocument<T>, bool>>x => x.FullDocument.Prop1 == "SomeValue"
batchSizeintThe max number of entities to receive for a single event occurence
onlyGetIDsboolSet to true if you don't want the complete entity details. All properties except the ID will then be null.
autoResumeboolSet to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
Start(EventType, Expression<Func<T, T>>, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, CancellationToken)
Starts the watcher instance with the supplied parameters. Supports projection.
public void Start(EventType eventTypes, Expression<Func<T, T>> projection, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool autoResume = true, CancellationToken cancellation = default)
Parameters
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
projectionExpression<Func<T, T>>A projection expression for the entity
filterFunc<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>b => b.Eq(d => d.FullDocument.Prop1, "value")
batchSizeintThe max number of entities to receive for a single event occurence
autoResumeboolSet to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
Start(EventType, Expression<Func<T, T>>, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, CancellationToken)
Starts the watcher instance with the supplied parameters. Supports projection.
public void Start(EventType eventTypes, Expression<Func<T, T>> projection, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool autoResume = true, CancellationToken cancellation = default)
Parameters
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
projectionExpression<Func<T, T>>A projection expression for the entity
filterExpression<Func<ChangeStreamDocument<T>, bool>>x => x.FullDocument.Prop1 == "SomeValue"
batchSizeintThe max number of entities to receive for a single event occurence
autoResumeboolSet to false if you'd like to skip the changes that happened while the watching was stopped. This will also make you unable to retrieve a ResumeToken.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
StartWithToken(BsonDocument, EventType, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, CancellationToken)
Starts the watcher instance with the supplied configuration
public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, bool onlyGetIDs = false, CancellationToken cancellation = default)
Parameters
resumeTokenBsonDocumentA resume token to start receiving changes after some point back in time
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
filterFunc<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>b => b.Eq(d => d.FullDocument.Prop1, "value")
batchSizeintThe max number of entities to receive for a single event occurence
onlyGetIDsboolSet to true if you don't want the complete entity details. All properties except the ID will then be null.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
StartWithToken(BsonDocument, EventType, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, bool, CancellationToken)
Starts the watcher instance with the supplied configuration
public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, bool onlyGetIDs = false, CancellationToken cancellation = default)
Parameters
resumeTokenBsonDocumentA resume token to start receiving changes after some point back in time
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
filterExpression<Func<ChangeStreamDocument<T>, bool>>x => x.FullDocument.Prop1 == "SomeValue"
batchSizeintThe max number of entities to receive for a single event occurence
onlyGetIDsboolSet to true if you don't want the complete entity details. All properties except the ID will then be null.
cancellationCancellationTokenA cancellation token for ending the watching/change stream
StartWithToken(BsonDocument, EventType, Expression<Func<T, T>>, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, CancellationToken)
Starts the watcher instance with the supplied configuration. Supports projection.
public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<T, T>> projection, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>> filter, int batchSize = 25, CancellationToken cancellation = default)
Parameters
resumeTokenBsonDocumentA resume token to start receiving changes after some point back in time
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
projectionExpression<Func<T, T>>A projection expression for the entity
filterFunc<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>b => b.Eq(d => d.FullDocument.Prop1, "value")
batchSizeintThe max number of entities to receive for a single event occurence
cancellationCancellationTokenA cancellation token for ending the watching/change stream
StartWithToken(BsonDocument, EventType, Expression<Func<T, T>>, Expression<Func<ChangeStreamDocument<T>, bool>>?, int, CancellationToken)
Starts the watcher instance with the supplied configuration. Supports projection.
public void StartWithToken(BsonDocument resumeToken, EventType eventTypes, Expression<Func<T, T>> projection, Expression<Func<ChangeStreamDocument<T>, bool>>? filter = null, int batchSize = 25, CancellationToken cancellation = default)
Parameters
resumeTokenBsonDocumentA resume token to start receiving changes after some point back in time
eventTypesEventTypeType of event to watch for. Specify multiple like: EventType.Created | EventType.Updated | EventType.Deleted
projectionExpression<Func<T, T>>A projection expression for the entity
filterExpression<Func<ChangeStreamDocument<T>, bool>>x => x.FullDocument.Prop1 == "SomeValue"
batchSizeintThe max number of entities to receive for a single event occurence
cancellationCancellationTokenA cancellation token for ending the watching/change stream
Events
OnChanges
This event is fired when the desired types of events have occured. Will have a list of 'entities' that was received as input.
public event Action<IEnumerable<T>>? OnChanges
Event Type
- Action<IEnumerable<T>>
OnChangesAsync
This event is fired when the desired types of events have occured. Will have a list of 'entities' that was received as input.
public event AsyncEventHandler<IEnumerable<T>>? OnChangesAsync
Event Type
OnChangesCSD
This event is fired when the desired types of events have occured. Will have a list of 'ChangeStreamDocuments' that was received as input.
public event Action<IEnumerable<ChangeStreamDocument<T>>>? OnChangesCSD
Event Type
- Action<IEnumerable<ChangeStreamDocument<T>>>
OnChangesCSDAsync
This event is fired when the desired types of events have occured. Will have a list of 'ChangeStreamDocuments' that was received as input.
public event AsyncEventHandler<IEnumerable<ChangeStreamDocument<T>>>? OnChangesCSDAsync
Event Type
- AsyncEventHandler<IEnumerable<ChangeStreamDocument<T>>>
OnError
This event is fired when an exception is thrown in the change-stream.
public event Action<Exception>? OnError
Event Type
OnStop
This event is fired when the internal cursor get closed due to an 'invalidate' event or cancellation is requested via the cancellation token.
public event Action? OnStop