Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Client for EventStore - Connection was closed

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 !

like image 500
Nayab Siddiqui Avatar asked Apr 28 '17 07:04

Nayab Siddiqui


1 Answers

I changed the version of the EventStore to 5.0.2 and resolved

like image 125
Arthur Cabral Avatar answered Sep 19 '22 12:09

Arthur Cabral