public GetEventStore(EventStoreClientSettings settings, IJsonSerializer serializer) { this.serializer = serializer; client = new EventStoreClient(settings); projectionClient = new EventStoreProjectionClient(settings, StreamPrefix); }
public GetEventStoreSubscription( IEventSubscriber subscriber, EventStoreClient client, EventStoreProjectionClient projectionClient, IJsonSerializer serializer, string?position, string?prefix, string?streamFilter) { Task.Run(async() => { var ct = cts.Token; var streamName = await projectionClient.CreateProjectionAsync(streamFilter); async Task OnEvent(StreamSubscription subscription, ResolvedEvent @event, CancellationToken ct) { var storedEvent = Formatter.Read(@event, prefix, serializer); await subscriber.OnEventAsync(this, storedEvent); } void OnError(StreamSubscription subscription, SubscriptionDroppedReason reason, Exception? ex) { if (reason != SubscriptionDroppedReason.Disposed && reason != SubscriptionDroppedReason.SubscriberError) { ex ??= new InvalidOperationException($"Subscription closed with reason {reason}."); subscriber.OnErrorAsync(this, ex); } } if (!string.IsNullOrWhiteSpace(position)) { var streamPosition = position.ToPosition(true); subscription = await client.SubscribeToStreamAsync(streamName, streamPosition, OnEvent, true, OnError, cancellationToken: ct); } else { subscription = await client.SubscribeToStreamAsync(streamName, OnEvent, true, OnError, cancellationToken: ct); } }, cts.Token); }