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

时间:2017-04-28 07:52:32

标签: c# .net-core get-event-store

我最近开始为.net-core(EventStore)试用nuget package客户端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

P.S。我也尝试使用.net客户端API,它似乎可以正常使用相同的代码。

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

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且能够使用以下内容:

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();
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

server:

   EVENTSTORE_DISABLE_INTERNAL_TCP_TLS=True
   EVENTSTORE_DISABLE_EXTERNAL_TCP_TLS=True

ConnectionString:

UseSslConnection=False

take a look here

或者您可以配置客户端以建立安全连接。