Search code examples
c#.neteventstoredb

EventStoreDB Catch-up subscription is disposed after reading first event


I'm using the EventStore gRPC client for .NET. If I connect to an empty EventStore the connection persists and all events that get appended afterwards are published to the subscribing client. When there are already events stored and I start the client, i.e. the client has to catch up to the current state, only the first event is being read and the connection is disposed.

EventStoreDB console log

Here is the configuration of the subscription.

await _client.SubscribeToAllAsync(
                FromAll.Start,
                async (subscription, @event, cancellationToken) =>
                {
                    // handle event
                },
                filterOptions: prefixStreamFilter);

Solution

  • You need to keep the subscription returned by SubscribeToAllAsync in a field, so it doesn't get disposed. When the subscription is disposed, it unsubscribes.

    The usual way to run a subscription continuously is to host it inside a hosted service or background service (which is a hosted service anyway).

    You can look at my implementation, but it might be a bit too complex to examine as it uses a few abstractions that are designed to support multiple subscription sources. You can also use Eventuous subscriptions (it's a NuGet package) as it takes care about a few things, which are kind of mandatory to do (drops, serialisation, etc).