/// <summary> /// Run server until exit /// </summary> private static async Task RunServerAsync(IEnumerable <int> ports) { using (var logger = StackLogger.Create(ConsoleLogger.Create())) { var tcs = new TaskCompletionSource <bool>(); AssemblyLoadContext.Default.Unloading += _ => tcs.TrySetResult(true); using (var server = new ServerConsoleHost(new ServerFactory(logger.Logger), logger.Logger) { AutoAccept = true }) { await server.StartAsync(ports); #if DEBUG if (!Console.IsInputRedirected) { Console.WriteLine("Press any key to exit..."); Console.TreatControlCAsInput = true; await Task.WhenAny(tcs.Task, Task.Run(() => Console.ReadKey())); return; } #endif await tcs.Task; logger.Logger.Information("Exiting."); } } }
/// <summary> /// Run server until cancelled /// </summary> private static async Task RunSampleServerAsync(CancellationToken ct, ILogger logger) { var tcs = new TaskCompletionSource <bool>(); ct.Register(() => tcs.TrySetResult(true)); using (var server = new ServerConsoleHost(new ServerFactory(logger) { LogStatus = false }, logger) { AutoAccept = true }) { logger.Information("Starting server."); await server.StartAsync(new List <int> { 51210 }); logger.Information("Server started."); await tcs.Task; logger.Information("Server exited."); } }