public PersistentSubscription Subscribe(string streamName, string groupName, Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared, Action <PersistentSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped = default, UserCredentials userCredentials = null, int bufferSize = 10, bool autoAck = true) { if (streamName == null) { throw new ArgumentNullException(nameof(streamName)); } if (groupName == null) { throw new ArgumentNullException(nameof(groupName)); } if (eventAppeared == null) { throw new ArgumentNullException(nameof(eventAppeared)); } if (streamName == string.Empty) { throw new ArgumentException($"{nameof(streamName)} may not be empty.", nameof(streamName)); } if (groupName == string.Empty) { throw new ArgumentException($"{nameof(groupName)} may not be empty.", nameof(groupName)); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } var options = new ReadReq.Types.Options { BufferSize = bufferSize, GroupName = groupName, StreamName = streamName, UuidOption = new ReadReq.Types.Options.Types.UUIDOption { Structured = new ReadReq.Types.Empty() } }; return(new PersistentSubscription(_client, options, autoAck, eventAppeared, subscriptionDropped ?? delegate { }, userCredentials)); }
internal static async Task <PersistentSubscription> Confirm(AsyncDuplexStreamingCall <ReadReq, ReadResp> call, ReadReq.Types.Options options, bool autoAck, Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared, Action <PersistentSubscription, SubscriptionDroppedReason, Exception?> subscriptionDropped, CancellationToken cancellationToken = default) { await call.RequestStream.WriteAsync(new ReadReq { Options = options }).ConfigureAwait(false); if (!await call.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false) || call.ResponseStream.Current.ContentCase != ReadResp.ContentOneofCase.SubscriptionConfirmation) { throw new InvalidOperationException(); } return(new PersistentSubscription(call, autoAck, eventAppeared, subscriptionDropped)); }
internal PersistentSubscription( PersistentSubscriptions.PersistentSubscriptionsClient client, ReadReq.Types.Options options, bool autoAck, Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared, Action <PersistentSubscription, SubscriptionDroppedReason, Exception> subscriptionDropped, UserCredentials userCredentials) { _client = client; _userCredentials = userCredentials; _options = options; _autoAck = autoAck; _eventAppeared = eventAppeared; _subscriptionDropped = subscriptionDropped; _disposed = new CancellationTokenSource(); _started = new TaskCompletionSource <bool>(); Task.Run(Start); }
public async Task <PersistentSubscription> SubscribeAsync(string streamName, string groupName, Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared, Action <PersistentSubscription, SubscriptionDroppedReason, Exception?>?subscriptionDropped = null, UserCredentials?userCredentials = null, int bufferSize = 10, bool autoAck = true, CancellationToken cancellationToken = default) { if (streamName == null) { throw new ArgumentNullException(nameof(streamName)); } if (groupName == null) { throw new ArgumentNullException(nameof(groupName)); } if (eventAppeared == null) { throw new ArgumentNullException(nameof(eventAppeared)); } if (streamName == string.Empty) { throw new ArgumentException($"{nameof(streamName)} may not be empty.", nameof(streamName)); } if (groupName == string.Empty) { throw new ArgumentException($"{nameof(groupName)} may not be empty.", nameof(groupName)); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } var operationOptions = Settings.OperationOptions.Clone(); operationOptions.TimeoutAfter = new TimeSpan?(); var call = new PersistentSubscriptions.PersistentSubscriptions.PersistentSubscriptionsClient( await SelectCallInvoker(cancellationToken).ConfigureAwait(false)).Read(EventStoreCallOptions.Create( Settings, operationOptions, userCredentials, cancellationToken)); var readOptions = new ReadReq.Types.Options { BufferSize = bufferSize, GroupName = groupName, UuidOption = new ReadReq.Types.Options.Types.UUIDOption { Structured = new Empty() } }; if (streamName == SystemStreams.AllStream) { readOptions.All = new Empty(); } else { readOptions.StreamIdentifier = streamName; } return(await PersistentSubscription.Confirm(call, readOptions, autoAck, eventAppeared, subscriptionDropped ?? delegate { }, cancellationToken).ConfigureAwait(false)); }