protected override async Task AcceptConnection(CancellationToken token) { var server = IOHelpers.NewNamedPipeServerStream(Settings.Name, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, GetPipeSecurity); try { // on linux WaitForConnectionAsync has to be cancelled with Dispose using (token.Register(server.Dispose)) { await server.WaitForConnectionAsync(); } // pass the ownership of the connection HandleConnection(server, callbackFactory => new Client(action => server.RunAsClient(() => action()), callbackFactory), token); } catch (Exception ex) { server.Dispose(); if (!token.IsCancellationRequested) { Logger.LogException(ex, Settings.Name); } } }