12

我最近开始尝试.net-core ( nuget package )的EventStore Client 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));
    }

这是我尝试写入 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);
    }
}

IEventSourced界面非常简单,如下所示:

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

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

PS 我也尝试过使用 .net Client API,它似乎使用相同的代码工作得很好。

任何指针将不胜感激。干杯!

4

3 回答 3

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();
}
于 2018-02-22T20:38:20.880 回答
1

我将 EventStore 的版本更改为 5.0.2 并解决

于 2020-11-13T19:44:57.997 回答
0

我遇到了同样的问题,在我的情况下服务器接受了安全 TLS 连接,您可以在如下配置中禁用它

server:

   EVENTSTORE_DISABLE_INTERNAL_TCP_TLS=True
   EVENTSTORE_DISABLE_EXTERNAL_TCP_TLS=True

ConnectionString:

UseSslConnection=False

看看这里

或者您可以配置客户端以进行安全连接。

于 2021-05-06T10:46:10.933 回答