IEventStoreConnection: should be used as singleton?

Hello guys,

Another beginner question regarding the usage of the IEventStoreConnection. According to the docs,

“The EventStoreConnection class is responsible for maintaining a full-duplex connection between the client and the event store server. EventStoreConnection is thread-safe, and it is recommended that only one instance per application is created.”

https://github.com/eventstore/eventstore/wiki/Overview-%28.NET-API%29

So, in my integration testes, I’ve thought about creating a static instance which should be used by all test methods. These test methods will usually start by opening the connection (Connect method) and by closing it up when they finish (Close method).

The first test runs just fine. However, all the others crash with the “cannot access a disposed object” when I try to open the connection.

To solve this, I had to call EventStoreConnection.Create in all the test methods. Now, should I be doing this? I mean, did I misread the docs? After all, don’t they say that I shoudl only create one instance of the IEventStoreConnection?

thanks for your patience.

In tests its common to use a connection per.

The connection per is only a performance difference (do you reconnect every time or do you maintain a single connection)

Hello Greg.

But should I get an exception while trying to open a connection that has been closed before? For instance, should I get an exception with this code:

var tcpEndpoint = new IPEndPoint( IPAddress.Loopback, 1120);

var connection = EventStoreConnection.Create(tcpEndpoint);

connection.Connect();

//do some work

connection.Close();

//later, try to open again

connection.Connect(); //throws here?

Btw, here’s the exception message:

Cannot access a disposed object. Object name: ‘ES-1897f1a7-55d7-4d02-954c-72d18ca8d483’.

Thanks,

Luis

We should probably make it a bit more friendly but yes closed connections should not be reconnected (they should be used then discarded)

Hello again.

We should probably make it a bit more friendly but yes closed connections

should not be reconnected (they should be used then discarded)

So, what you're saying is that I should create a single connection and open
it when my app begins and then close it when the app exits?

Thanks,

Thats an ideal usage it handles reconnections etc for you and is threadsafe/meant to be used from multiple threads

Hello again Greg.