Maxim Kitsenko
Maxim Kitsenko

Reputation: 2333

Read stream from EventStore without "stream doesn't exist exception"

Application written in .net tries to read event stream. For this goal, EventStoreCleint wrapper was written, under the hood it uses EventStore.Client.Grpc.Streams 23.1.0. The logic is the following:

The problem is in fact, when the application reads the event stream by _client.ReadStreamAsync if there is no stream, the exception will be thrown. Exceptions are expensive from the performance point of view.

How to read stream even is stream version is 0 without exceptions?

Upvotes: 2

Views: 139

Answers (2)

Ruben Bartelink
Ruben Bartelink

Reputation: 61795

If you inspect the StreamState in the result, it will be StreamNotFound

var res = await conn.ReadStreamAsync(Direction.Forwards, streamName, startPosition, Int64.MaxValue, resolveLinkTos: false, cancellationToken: ct)
if (res.ReadState == ReadState.StreamNotFound)
   // TODO handle empty

Example in Equinox

When writing, ConditionalAppendToStreamAsync don't throw an exception either; you inspect the Status of the result.

Example in Equinox

Upvotes: 1

Maxim Kitsenko
Maxim Kitsenko

Reputation: 2333

Key thing is to not enumerate events immediately e.g.:

_client.ReadStreamAsync(Direction.Forwards, name, StreamPosition.Start).ToListAsync().Result

Instead use something like this:

var res = await _client.ReadStreamAsync(Direction.Forwards, name, StreamPosition.Start);
if (res.ReadState == ReadState.Ok)
    events = res.ToListAsync();

Upvotes: 0

Related Questions