Exemple #1
0
        static void Main(string[] args)
        {
            var noSql = new MFlixNoSqlDbContext(
                "mongodb+srv://######:########@freecluster-omk9a.mongodb.net/test?retryWrites=true&w=majority",
                "sample_mflix"
                );

            FilterDefinition <ChangeStreamDocument <Movie> > filterBuilder =
                Builders <ChangeStreamDocument <Movie> > .Filter
                .In(x => x.OperationType, new[] { ChangeStreamOperationType.Insert }.ToList());

            var options = new ChangeStreamOptions {
                FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
            };

            PipelineDefinition <ChangeStreamDocument <Movie>, ChangeStreamDocument <Movie> > pipeline =
                new EmptyPipelineDefinition <ChangeStreamDocument <Movie> >()
                .Match <ChangeStreamDocument <Movie>, ChangeStreamDocument <Movie> >(filterBuilder);

            IChangeStreamCursor <ChangeStreamDocument <Movie> > cursor = noSql.GetCollection <Movie>("movies")
                                                                         .Watch(pipeline, options);

            using (IEnumerator <ChangeStreamDocument <Movie> > enumerator = cursor.ToEnumerable().GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    ChangeStreamDocument <Movie> doc = enumerator.Current;
                    Console.WriteLine(doc?.DocumentKey);
                    Console.WriteLine(doc?.FullDocument.Title);
                }
            }

            Console.WriteLine("Done!");
        }
Exemple #2
0
        void startListeningToNotification(CancellationToken cancellationToken)
        {
            try
            {
                using (IChangeStreamCursor <ChangeStreamDocument <Notification> > cursor = _collection.Watch(_userLogin, cancellationToken))
                {
                    try
                    {
                        // might want to actually create an event inside the DAO
                        //   that way we are not depending on IChangeStreamCursor in the service
                        //    this might make unit tests much harder
                        //  Could also return cursor.ToEnumerable(cancellationToken) inside the DAO that way it simply returns IEnumerable<ChangeStreamDocument<Notification>>
                        //   ChangeStreamDocument<Notification> can easily be faked by creating a bson document
                        foreach (var notif in cursor.ToEnumerable(cancellationToken))
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            OnNewNotification?.Invoke(new NewNotificationEventArgs(
                                                          new NewNotificationDTO
                            {
                                Id      = notif.FullDocument.Id,
                                Message = notif.FullDocument.Message,
                                DtgUtc  = notif.FullDocument.EventDtgUtc,
                                Type    = notif.FullDocument.Type
                            }
                                                          ));
                        }
                    }
                    catch (OperationCanceledException ex)
                    {
                        Console.WriteLine("Operation was canceled");
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                        notificationServiceErrorCount++;

                        if (notificationServiceErrorCount > 2)
                        {
                            throw ex;
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Notification service cancellation requested");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Notification service error:");
                Console.WriteLine(ex);
            }
        }