用于EventStore的.Net核心客户端 - 连接已关闭

Nay*_*qui 8 c# get-event-store .net-core

我最近开始尝试使用.net-core(nuget包)的EventStore客户端API .但是,我正在努力让事件写入流中.下面是我用来建立连接的代码:

    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));
    }
Run Code Online (Sandbox Code Playgroud)

这是我试图写入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);
    }
}
Run Code Online (Sandbox Code Playgroud)

IEventSourced接口是相当简单的,是如下:

public interface IEvenSourced
{
    Guid Id { get; }
    IEnumerable<Event> PendingChanges { get; }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我调用该SaveAsync函数时,我经常遇到异常:EventStore.ClientAPI.Exceptions.ConnectionClosedException : Connection 'ES-9105371b-bf54-4e18-98e7-d67e4ce11aef' was closed.

PS我也尝试使用.net客户端API,它似乎与相同的代码工作得很好.

任何指针都将非常感激.干杯!

yoh*_*kin 3

我遇到了同样的问题,并且能够使其与以下内容一起工作:

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();
}
Run Code Online (Sandbox Code Playgroud)