Skip to content

Instantly share code, notes, and snippets.

@riccardone
Created August 8, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardone/2e8e00317becfde2874b8e810979a2cd to your computer and use it in GitHub Desktop.
Save riccardone/2e8e00317becfde2874b8e810979a2cd to your computer and use it in GitHub Desktop.
EventStore C# TestClusterConnection App
namespace TestClusterConnection
{
class Program
{
private const string Stream = "MyTestStream";
static void Main(string[] args)
{
try
{
var useCluster = true;
Console.WriteLine("Start connecting to Eventstore");
Write(useCluster, Stream).Wait();
Console.WriteLine("Start reading events:");
Read(useCluster, Stream).Wait();
Console.WriteLine("Check if events are written and press key to exit");
}
catch (Exception e)
{
Console.WriteLine(e.GetBaseException().Message);
}
Console.ReadKey();
}
public static async Task Read(bool useCluster, string streamName)
{
using (var conn = CreateConnection(useCluster))
{
await conn.ConnectAsync();
var slice = await conn.ReadStreamEventsForwardAsync(streamName, 0, 100, false);
foreach (var evt in slice.Events)
Console.WriteLine($"Received event. Type: '{evt.Event.EventType}', Data: '{Encoding.UTF8.GetString(evt.Event.Data)}'");
}
}
private static async Task Write(bool useCluster, string streamName)
{
using (var conn = CreateConnection(useCluster))
{
await conn.ConnectAsync();
for (var x = 0; x < 100; x++)
{
await conn.AppendToStreamAsync(streamName,
ExpectedVersion.Any,
GetEventDataFor(x));
Console.WriteLine("event " + x + " written.");
}
}
}
private static IEnumerable<EventData> GetEventDataFor(int i)
{
yield return new EventData(
Guid.NewGuid(),
"MyTestEvent",
true,
Encoding.ASCII.GetBytes("{'somedata' : " + i + "}"),
Encoding.ASCII.GetBytes("{'metadata' : " + i + "}")
);
}
private static IEventStoreConnection SingleConnection()
{
return EventStoreConnection.Create(ConnectionSettings.Create(),
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1113));
}
private static IEventStoreConnection ClusterConnection()
{
return EventStoreConnection.Create(
ConnectionSettings.Create().KeepRetrying().KeepReconnecting().UseConsoleLogger()
.SetGossipSeedEndPoints(
new IPEndPoint(IPAddress.Parse("172.19.0.2"), 2112),
new IPEndPoint(IPAddress.Parse("172.19.0.3"), 2112),
new IPEndPoint(IPAddress.Parse("172.19.0.4"), 2112))
.SetHeartbeatInterval(TimeSpan.FromSeconds(3))
.SetHeartbeatTimeout(TimeSpan.FromSeconds(6))
.WithConnectionTimeoutOf(TimeSpan.FromSeconds(10))
);
}
private static IEventStoreConnection CreateConnection(bool useCluster)
{
return useCluster ? ClusterConnection() : SingleConnection();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment