Simple console example.
See the lines preceded by #warning
Can you get either of the ‘awaited’ calls to work? I always get a “Lost connection” message on my server console.
The .Wait() version always runs to completion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EventStore.ClientAPI;
using EventStore.ClientAPI.SystemData;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Repro
{
class Program
{
private static string _testStreamName = "TestEvents-" + Guid.NewGuid();
private static string _subscriptionStreamName = "$ce-TestEvents";
private static string _subscriptionGroupName = "TestEvents-" + Guid.NewGuid();
private static string _filePath = _subscriptionGroupName;
public static Uri EsEndpoint = new Uri("tcp://localhost:1113");
public static UserCredentials Creds = new UserCredentials("admin", "changeit");
static void Main()
{
Console.WriteLine("Main()");
Task.Run(async () =>
{
await MainAsync().ConfigureAwait(false);
}).Wait();
Console.WriteLine("Main() Complete");
}
private static async Task MainAsync()
{
Console.WriteLine("\tMainAsync()");
ConnectionSettingsBuilder connectionSettingsBuilder = ConnectionSettings.Create();
ConnectionSettings connectionSettings = connectionSettingsBuilder.Build();
var conn = EventStoreConnection.Create(connectionSettings, EsEndpoint);
conn.ConnectAsync().Wait();
await Test(conn);
Console.WriteLine("\n\tDelay in MainAsync : start");
await Task.Delay(10000);
Console.WriteLine("\n\tDelay in MainAsync : done");
}
private static async Task Test(IEventStoreConnection conn)
{
var settings =
PersistentSubscriptionSettings.Create().ResolveLinkTos().StartFromBeginning();
Console.WriteLine("Creating Subscription");
#warning Only the .Wait() version creates a working program
//conn.CreatePersistentSubscriptionAsync(_subscriptionStreamName, _subscriptionGroupName, settings, Creds).Wait();
#warning The following two statements result in a lost connection / halted program
await conn.CreatePersistentSubscriptionAsync(_subscriptionStreamName,_subscriptionGroupName,settings,Creds);
//await conn.CreatePersistentSubscriptionAsync(_subscriptionStreamName, _subscriptionGroupName, settings, Creds).ConfigureAwait(false);
Console.WriteLine("Created Subscription");
Console.WriteLine("Connecting to Subscription");
conn.ConnectToPersistentSubscription(
_subscriptionStreamName,
_subscriptionGroupName,
//(sub, e) => Resolve(sub, e),
(sub, e) => Console.WriteLine("Event"),
(sub, reason, ex) =>
{
Console.WriteLine(reason);
Console.WriteLine(ex);
},
autoAck: false,
userCredentials: Creds);
Console.WriteLine("Connected to Subscription");
Console.WriteLine("\n\t Delay from Test : start");
await Task.Delay(1000);
Console.WriteLine("\n\t Delay from Test : done");
}
public static void ColoredConsoleWrite(ConsoleColor color, string text)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ForegroundColor = originalColor;
}
}
}
``