Error recovery strategies for consuming clients (C#)

Hello:

We’re just starting off on a project that’s utilising EventStore, and I’ve been working on a client to consume events in C#. One bit I’m uncertain about is how best to handle internal application exceptions (not errors with the EventStore connection, but other sources). I’ve got an OnEventAppeared block similar to this:

private void StartProjection()

{

subscription = _conn.SubscribeToStreamFrom(

streamName,

checkpoint,

settings,

OnEventReceived());

}

private Action<EventStoreCatchUpSubscription, ResolvedEvent> OnEventReceived()

{

return (sub, e) =>

{

try

{

// Process event

}

catch (Exception ex)

{

// Log exception

throw;

}

};

}

The final throw seems to stop further reads from the stream, which is what I want in this circumstance. But what I’d like to do at that point is start into an alerting/recovery/shutdown cycle so I can either restart reading from an appropriate point, or go into an orderly shutdown of the reader application. I was wondering if there were any events in the client library that could subscribe to that would be triggered in this case? Or if there were any other suggestions or best practices on how to react to exceptions in the event appeared callback?

Any other suggestions or examples on how best to code an EventStore client for exception recovery are welcome.

Thanks.

Robert Goheen

In the case of CatchupSubscriptions, if there is an error thrown while processing events the subscription will be dropped.
You can use subscriptionDropped for error handling. For example:

conn.SubscribeToStreamFrom(
streamName,
checkpoint,
settings,
OnEventReceived,
subscriptionDropped: (sub, reason, ex) =>
{
// Handle the errors, resubscribe, etc.
});