public async Task Invoke(HttpContext context) { if (!context.WebSockets.IsWebSocketRequest) { // Not a web socket request await _next.Invoke(context); return; } var ct = context.RequestAborted; using (var socket = await context.WebSockets.AcceptWebSocketAsync()) { while (socket.State == WebSocketState.Open) { if (_cardReader.IsCardAvailable()) { await SendStringAsync(socket, BitConverter.ToString(_cardReader.GetCardId()), ct); } await Task.Delay(TimeSpan.FromMilliseconds(100)); } } }