public async Task Invoke(HttpContext context) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var request = await FormatRequest(context.Request); var originalBodyStream = context.Response.Body; using (var responseBody = new MemoryStream()) { context.Response.Body = responseBody; await _next(context); var response = await FormatResponse(context.Response); await responseBody.CopyToAsync(originalBodyStream); if (response.Contains("\"id\":0")) {/*new query*/ stopwatch.Stop(); //Task.Factory.StartNew(() => //{ string Message = "New Request:" + context.Request.QueryString + ", Duration:" + stopwatch.ElapsedMilliseconds.ToString() + " miliseconds."; _chatHubDispatcher.SendAllClients(DateTime.Now.ToString("dd:MM:yyyy HH:MM:ss:fff") + Message, response); //}); } Task.Factory.StartNew(() => { InMemory.Flush(); }); } }