Event Store - Read a Single Event with .NET API

要使用 Event Store .NET API 讀取 Event Store 特定 Stream 內特定的 Event,可以帶入 Stream 的名稱、Event 的編號,調用 Connection.ReadEventAsync 方法。

1
2
3
...
var readResult = conn.ReadEventAsync(streamName, 0, true).Result;
...


然後再去讀取需要的 Event 資料即可。

1
2
3
...
Console.WriteLine("{0} {1}", readResult.EventNumber, Encoding.UTF8.GetString(readResult.Event.Value.Event.Data));
...


像是筆者這邊有個 Event 如下:


就可以像下面這樣讀取指定的 Event。

1
2
3
4
5
6
7
8
9
10
11
using EventStore.ClientAPI;
...
using (var conn = EventStoreConnection.Create(connectionString, connectionName))
{
conn.ConnectAsync().Wait();

var streamName = "MyStream";
var readResult = conn.ReadEventAsync(streamName, 0, true).Result;
Console.WriteLine("{0} {1}", readResult.EventNumber, Encoding.UTF8.GetString(readResult.Event.Value.Event.Data));
}
...