dj-nitehawk / MongoDB.Entities

A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
https://mongodb-entities.com
MIT License
547 stars 70 forks source link

Change Stream functionality doesn't work #196

Closed samlanza closed 1 year ago

samlanza commented 1 year ago

When I setup the watcher per the instructions:

watcher.Start( eventTypes: EventType.Created | EventType.Updated | EventType.Deleted, filter: null, batchSize: 25, onlyGetIDs: false, autoResume: true, cancellation: default);

I get the following 'ambiguous' build error:

Error CS0121: The call is ambiguous between the following methods or properties: 'Watcher<T>.Start(EventType, Expression<Func<ChangeStreamDocument<T>, bool>>, int, bool, bool, CancellationToken)' and 'Watcher<T>.Start(EventType, Func<FilterDefinitionBuilder<ChangeStreamDocument<T>>, FilterDefinition<ChangeStreamDocument<T>>>, int, bool, bool, CancellationToken)'

I can fix the build error by simply removing the "filter" option:

watcher.Start( eventTypes: EventType.Created | EventType.Updated | EventType.Deleted, batchSize: 25, onlyGetIDs: false, autoResume: true, cancellation: default);

However, when I execute the console app, it falls right through the watcher and nothing happens. Here's the full code:

await DB.InitAsync("Google", MongoClientSettings.FromConnectionString( "connection_string_redacted"));

var watcher = DB.Watcher("employee-watcher");

watcher.Start( eventTypes: EventType.Created | EventType.Updated | EventType.Deleted, batchSize: 25, onlyGetIDs: false, autoResume: true, cancellation: default);

watcher.OnChanges += employees => { foreach (var employee in employees) { Console.WriteLine("received: " + employee.Name); } };

watcher.OnError += exception => { Console.WriteLine("error: " + exception.Message); if (watcher.CanRestart) { watcher.ReStart(); Console.WriteLine("Watching restarted!"); } };

watcher.OnStop += () => { Console.WriteLine("Watching stopped!"); if (watcher.CanRestart) { watcher.ReStart(); Console.WriteLine("Watching restarted!"); } else { Console.WriteLine("This watcher is dead!"); } };

samlanza commented 1 year ago

I actually figured it out. You have to add an endless loop after setting up the watcher and the delegate actions.