14

I've recently started trying out the EventStore Client API for .net-core (nuget package). However, I'm struggling in getting the events to write into the stream. Below is the code that I'm using to establish the connection:

    private readonly string _eventStoreName = "localhost";
    private readonly string _eventStorePort = "1113";
    protected IEventStoreConnection Connection;

    public EventStoreTestFixture()
    {
        var eventStoreIpAddress = Dns.GetHostAddressesAsync(_eventStoreName).Result;
        var ipAddress = GetIpAddressFromHost(eventStoreIpAddress);
        var connectionSettings = ConnectionSettings.Create()
            .SetDefaultUserCredentials(new UserCredentials("admin", "changeit"))
            .EnableVerboseLogging();
        Connection = EventStoreConnection.Create(connectionSettings,new IPEndPoint(ipAddress, int.Parse(_eventStorePort)));
        Connection.ConnectAsync().Wait();
    }

    private static IPAddress GetIpAddressFromHost(IPAddress[] eventStoreIpAddress)
    {
        return
            eventStoreIpAddress.FirstOrDefault(
                ipAddress => ipAddress.AddressFamily.Equals(AddressFamily.InterNetwork));
    }

And here is the code where I'm attempting to write to the EventStream:

public class EmployeeEventSourcedRepository<T> where T : class, IEvenSourced
{
    private readonly IEventStoreConnection _connection;

    public EmployeeEventSourcedRepository(IEventStoreConnection connection)
    {
        _connection = connection;
    }

    public async Task SaveAsync(T eventSourced)
    {
        var eventsToSave =
            eventSourced.PendingChanges
                .Select(change => new EventData(Guid.NewGuid(),
                    change.GetType().Name,
                    true,
                    Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(change, Formatting.None)),
                    new byte[]{}));

        var streamId = $"{eventSourced.GetType().Name.ToLowerInvariant()}-{eventSourced.Id}";
        await _connection.AppendToStreamAsync(streamId, ExpectedVersion.Any, eventsToSave);
    }
}

The IEventSourced interface is quite straightforward and is as below:

public interface IEvenSourced
{
    Guid Id { get; }
    IEnumerable<Event> PendingChanges { get; }
}

Now, when I invoke the SaveAsync function, I'm constantly running into the exception : EventStore.ClientAPI.Exceptions.ConnectionClosedException : Connection 'ES-9105371b-bf54-4e18-98e7-d67e4ce11aef' was closed.

P.S. I tried using the .net Client API as well, and it seems to work just fine with the same code.

Any pointers would be greatly appreciated. Cheers !

mat-mcloughlin
  • 6,492
  • 12
  • 45
  • 62
Nayab Siddiqui
  • 1,971
  • 1
  • 15
  • 18

3 Answers3

3

I was having the same issue and was able to get it working with the following:

public EventStore()
{
    _connection = EventStoreConnection.Create(
        new IPEndPoint(
            IPAddress.Parse("127.0.0.1"),
            1113
        )
    );
    _connection.ConnectAsync().Wait();
}

public void Publish(DomainEvent domainEvent)
{
    _connection.AppendToStreamAsync(
        "stream-name",
        ExpectedVersion.Any,
        new EventData(
            Guid.NewGuid(),
            domainEvent.GetType().FullName,
            false, 
            Encoding.UTF8.GetBytes(domainEvent.ToString()),
            new byte[] {}
        )
    ).Wait();
}
yohanmishkin
  • 158
  • 12
1

I changed the version of the EventStore to 5.0.2 and resolved

0

I had the same issue, in my case server accepted secure-TLS connection, you can disable it in configurations like below

server:

   EVENTSTORE_DISABLE_INTERNAL_TCP_TLS=True
   EVENTSTORE_DISABLE_EXTERNAL_TCP_TLS=True

ConnectionString:

UseSslConnection=False

take a look here

or you can configure client to make a secure-connection.