Recommended strategy for using IEventStoreConnection in web app

Hello guys.

Quick question: is there any recommended strategy for creating IEventStoreConnection objects in a web app? I’ve configured it as a singleton (using autofac), but I’ve noticed that my code will always throw a Event store connection xyz is not active error when I try to save or load events. Not sure if it helps, but here’s the code I’m using:

public class DepositoEventos : IDepositoEventos {

private const int _numeroEventosPorPagina = 200;

private readonly IEventStoreConnection _ligacaoDeposito;

private readonly ISeriadorEventos _seriadorEventos;

public DepositoEventos(IEventStoreConnection ligacaoDeposito, ISeriadorEventos seriadorEventos) {

Contract.Requires(ligacaoDeposito != null);

Contract.Requires(seriadorEventos != null);

Contract.Ensures(_ligacaoDeposito != null);

_ligacaoDeposito = ligacaoDeposito;

_seriadorEventos = seriadorEventos;


public async Task GravaEventos(string idStream, string tipo, IEnumerable eventosGravar, int versaoEsperada) {

var eventosPreviamenteGravadosEmConflitoEventual = await ObtemEventosParaAgregado(idStream, versaoEsperada + 1);

if (eventosGravar.Any(eventoGravar => eventosPreviamenteGravadosEmConflitoEventual.Any(eventoGravado => ExisteConflitoEventos(eventoGravar, eventoGravado)))) {

throw new ConcurrencyException();


//update event number

var evtVersionNumber = eventosPreviamenteGravadosEmConflitoEventual.Any() ? eventosPreviamenteGravadosEmConflitoEventual.Max(evt => evt.Versao) : versaoEsperada;

/*foreach (var evento in eventosGravar) {

evento.Versao = ++evtVersionNumber;


try {

using (var tran = await _ligacaoDeposito.StartTransactionAsync(idStream, evtVersionNumber)) {

await tran.WriteAsync(

eventosGravar.Select(eventoGravar => new EventData(Guid.NewGuid(),





await tran.CommitAsync();



catch (WrongExpectedVersionException ex) {

throw new ConcurrencyException("", ex);



public async Task<IEnumerable> ObtemEventosParaAgregado(string idStream, int versaoEsperada = StreamPosition.Start) {

var eventos = new List();

if (versaoEsperada < 0) {

return eventos;


try {

var indiceProxPaginaElementos = versaoEsperada;

StreamEventsSlice paginaElementos = null;

do {

paginaElementos = await _ligacaoDeposito.ReadStreamEventsForwardAsync(idStream, indiceProxPaginaElementos, _numeroEventosPorPagina, false);


indiceProxPaginaElementos = paginaElementos.NextEventNumber;

} while (!paginaElementos.IsEndOfStream);


catch (Exception ex) {

var t = “”;


return eventos;


protected virtual bool ExisteConflitoEventos(IEvento eventoGravar, IEvento eventoGravado) {

return eventoGravado.GetType() == eventoGravar.GetType();



Here’s how it’s getting instantiated:

builder.Register(c => EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, ObtemPortaTcp())))



This is a simple mvc app which also has another singleton component which is responsible for subscribing to the store (by using a projectionsmanager). I’ve written similar code to this in the past and it used to work well (though at the time, I did use version 2,.0).

Any clues on why I’m always getting this error? has anything changed regarding the way we use the .NET API to write/read events from the event store?




Fixed it…rookie mistake: the connection was not being opened anywhere.


Ah yes, they need to be opened to work! :slight_smile:

Good to see it’s fixed!