private Task <StreamSubscription> SubscribeToStreamAsync(string streamName,
                                                                 StreamRevision start,
                                                                 Func <StreamSubscription, ResolvedEvent, CancellationToken, Task> eventAppeared,
                                                                 EventStoreClientOperationOptions operationOptions,
                                                                 bool resolveLinkTos = false,
                                                                 Action <StreamSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped = default,
                                                                 UserCredentials userCredentials     = default,
                                                                 CancellationToken cancellationToken = default)
        {
            operationOptions.TimeoutAfter = DeadLine.None;

            return(StreamSubscription.Confirm(ReadInternal(new ReadReq {
                Options = new ReadReq.Types.Options {
                    ReadDirection = ReadReq.Types.Options.Types.ReadDirection.Forwards,
                    ResolveLinks = resolveLinkTos,
                    Stream = start == StreamRevision.End
                                                                ? new ReadReq.Types.Options.Types.StreamOptions {
                        End = new Empty(),
                        StreamName = streamName
                    }
                                                                : new ReadReq.Types.Options.Types.StreamOptions {
                        Revision = start,
                        StreamName = streamName
                    },
                    Subscription = new ReadReq.Types.Options.Types.SubscriptionOptions()
                }
            },
                                                           operationOptions, userCredentials, cancellationToken), eventAppeared, subscriptionDropped, _log,
                                              cancellationToken: cancellationToken));
        }
Ejemplo n.º 2
0
        private StreamSubscription SubscribeToStream(string streamName,
                                                     StreamRevision start,
                                                     Func <StreamSubscription, ResolvedEvent, CancellationToken, Task> eventAppeared,
                                                     EventStoreClientOperationOptions operationOptions,
                                                     bool resolveLinkTos = false,
                                                     Action <StreamSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped = default,
                                                     UserCredentials userCredentials     = default,
                                                     CancellationToken cancellationToken = default)
        {
            operationOptions.TimeoutAfter = DeadLine.None;

            var subscription = new StreamSubscription(ReadInternal(new ReadReq {
                Options = new ReadReq.Types.Options {
                    ReadDirection = ReadReq.Types.Options.Types.ReadDirection.Forwards,
                    ResolveLinks  = resolveLinkTos,
                    Stream        = ReadReq.Types.Options.Types.StreamOptions.FromStreamNameAndRevision(streamName,
                                                                                                        start),
                    Subscription = new ReadReq.Types.Options.Types.SubscriptionOptions()
                }
            },
                                                                   operationOptions, userCredentials, cancellationToken), eventAppeared,
                                                      subscriptionDropped);

            return(subscription);
        }
Ejemplo n.º 3
0
 private void HandleDrop(
     StreamSubscription arg1,
     SubscriptionDroppedReason arg2,
     Exception?arg3)
 {
     _log.LogWarning($"Subscription {SubscriptionId} dropped. Reason: {arg2}");
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Wrap event handling and checkpoint updates in a transaction.
        /// </summary>
        /// <param name="_"></param>
        /// <param name="e"></param>
        /// <param name="ct"></param>
        private async Task TransactionalHandler(
            StreamSubscription _,
            ResolvedEvent e,
            CancellationToken ct)
        {
            try
            {
                _log.LogDebug($"Subscription {SubscriptionId} got an event {e.Event.EventType}");
                using var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

                await Handler(AsReceivedEvent(e), ct);

                tx.Complete();
            }
            catch (Exception exception)
            {
                _log.LogError(exception.ToString());
                throw;
            }

            ReceivedEvent AsReceivedEvent(ResolvedEvent re)
            {
                var evt = DeserializeData(
                    re.Event.ContentType,
                    re.Event.EventType,
                    re.Event.Data,
                    re.Event.EventStreamId,
                    re.Event.EventNumber
                    );

                return(new ReceivedEvent(
                           re.Event.EventId.ToString(),
                           re.Event.EventType,
                           re.Event.ContentType,
                           re.Event.Position.CommitPosition,
                           re.Event.Position.CommitPosition,
                           re.OriginalStreamId,
                           re.Event.EventNumber,
                           re.Event.Created,
                           evt
                           ));
            }
        }
        private Task <StreamSubscription> SubscribeToAllAsync(Position start,
                                                              Func <StreamSubscription, ResolvedEvent, CancellationToken, Task> eventAppeared,
                                                              EventStoreClientOperationOptions operationOptions,
                                                              bool resolveLinkTos = false,
                                                              Action <StreamSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped = default,
                                                              SubscriptionFilterOptions filterOptions = null,
                                                              UserCredentials userCredentials         = default,
                                                              CancellationToken cancellationToken     = default)
        {
            operationOptions.TimeoutAfter = DeadLine.None;

            return(StreamSubscription.Confirm(ReadInternal(new ReadReq {
                Options = new ReadReq.Types.Options {
                    ReadDirection = ReadReq.Types.Options.Types.ReadDirection.Forwards,
                    ResolveLinks = resolveLinkTos,
                    All = ReadReq.Types.Options.Types.AllOptions.FromPosition(start),
                    Subscription = new ReadReq.Types.Options.Types.SubscriptionOptions(),
                    Filter = GetFilterOptions(filterOptions)
                }
            }, operationOptions, userCredentials, cancellationToken), eventAppeared, subscriptionDropped, _log,
                                              filterOptions?.CheckpointReached, cancellationToken));
        }
Ejemplo n.º 6
0
        private StreamSubscription SubscribeToAll(
            Func <StreamSubscription, ResolvedEvent, CancellationToken, Task> eventAppeared,
            EventStoreClientOperationOptions operationOptions,
            bool resolveLinkTos = false,
            Action <StreamSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped = default,
            IEventFilter filter                 = null,
            UserCredentials userCredentials     = default,
            CancellationToken cancellationToken = default)
        {
            operationOptions.TimeoutAfter = DeadLine.None;

            var subscription = new StreamSubscription(ReadInternal(new ReadReq {
                Options = new ReadReq.Types.Options {
                    ReadDirection = ReadReq.Types.Options.Types.ReadDirection.Forwards,
                    ResolveLinks  = resolveLinkTos,
                    All           = ReadReq.Types.Options.Types.AllOptions.FromPosition(Position.Start),
                    Subscription  = new ReadReq.Types.Options.Types.SubscriptionOptions(),
                    Filter        = GetFilterOptions(filter)
                }
            }, operationOptions, userCredentials, cancellationToken), eventAppeared,
                                                      subscriptionDropped);

            return(subscription);
        }
Ejemplo n.º 7
0
 private async Task SubscribeReturn(EventStore.Client.StreamSubscription ss, ResolvedEvent vnt, CancellationToken ct)
 {
     await SayHello(vnt.ToJson());
 }