Event Store - Read a Stream of Events with .NET API
要使用 Event Store .NET API 讀取 Event Store 特定 Stream 內的 Event,可以帶入 Stream 的名稱、起始的 Event 編號、以及預計要讀取的 Event 數,去調用 Connetction.ReadStreamEventsForwardAsync 方法。
...
var readEvents = conn.ReadStreamEventsForwardAsync(streamName, start, count, true).Result;
...
然後再去讀取需要的 Event 資料即可。
...
foreach (var evt in readEvents.Events)
Console.WriteLine("{0} {1}", evt.Event.EventNumber, Encoding.UTF8.GetString(evt.Event.Data));
...
像是這邊筆者有個 Stream 內含有 100 個相同資料的 Event。
就可以像下面這樣讀取特定範圍的 Event。
using EventStore.ClientAPI;
...
using (var conn = EventStoreConnection.Create(connectionString, connectionName))
{
conn.ConnectAsync().Wait();
var streamName = "MyStream";
var readEvents = conn.ReadStreamEventsForwardAsync(streamName, 10, 10, true).Result;
foreach (var evt in readEvents.Events)
Console.WriteLine("{0} {1}", evt.Event.EventNumber, Encoding.UTF8.GetString(evt.Event.Data));
}
...