title: ASP.NET Core 3.1 slow responses
body: <p>I have a standard ASP.NET Core 3.1 API application, which uses MVC controllers. The code is pretty straightforward, from the controller the code makes a query to MongoDb to retrieve a document. This is fast and pretty much okay. However, when I turned diagnostics on, I am seeing the request takes much longer than it should, the controller finishes it's work at 177ms while the final request finishes at 1068ms with a 204 response. See below for more information.</p>
  <pre><code>2020-09-22 18:16:48.5629|2|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Executed action method MyProject.API.Controllers.MyController.Post (MyProject.API), 
  returned result Microsoft.AspNetCore.Mvc.StatusCodeResult in 177.3325ms.
  
  2020-09-22 18:16:48.5938|4|TRACE|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Before executing action result Microsoft.AspNetCore.Mvc.StatusCodeResult.
  
  2020-09-22 18:16:48.6289|1|INFO|Microsoft.AspNetCore.Mvc.StatusCodeResult|
  Executing HttpStatusCodeResult, setting HTTP status code 204
  
  2020-09-22 18:16:48.6430|5|TRACE|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  After executing action result Microsoft.AspNetCore.Mvc.StatusCodeResult.
  
  2020-09-22 18:16:48.6754|2|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Executed action MyProject.API.Controllers.MyController.Post (MyProject.API) in 735.1339ms
  
  2020-09-22 18:16:48.6962|1|INFO|Microsoft.AspNetCore.Routing.EndpointMiddleware|
  Executed endpoint 'MyProject.API.Controllers.MyController.Post (MyProject.API)'
  
  2020-09-22 18:16:48.7031|37|TRACE|Microsoft.AspNetCore.Server.Kestrel|
  Connection id &quot;0HM2V3CUT1GMI&quot; sending HEADERS frame for stream 
  ID 15 with length 53 and flags END_STREAM, END_HEADERS
  
  2020-09-22 18:16:48.7031||2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|
  Request finished in 1068.2472ms 204
  
  </code></pre>
  <p>That's a huge difference there, approximately 890ms!
  The above results are taken from a cold instance, so this was the first request that was received.
  On a warm instance the difference is still significant (but not on the same scale).</p>
  <pre><code>2020-09-22 18:24:24.9553|2|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Executed action method MyProject.API.Controllers.MyController.Get (MyProject.API), 
  returned result Microsoft.AspNetCore.Mvc.StatusCodeResult in 11.4986ms.
  
  2020-09-22 18:24:24.9553|4|TRACE|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Before executing action result Microsoft.AspNetCore.Mvc.StatusCodeResult.
  
  2020-09-22 18:24:24.9553|1|INFO|Microsoft.AspNetCore.Mvc.StatusCodeResult|
  Executing HttpStatusCodeResult, setting HTTP status code 204
  
  2020-09-22 18:24:24.9553|5|TRACE|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  After executing action result Microsoft.AspNetCore.Mvc.StatusCodeResult.
  
  2020-09-22 18:24:24.9553|2|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|
  Executed action MyProject.API.Controllers.MyController.Get (MyProject.API) in 36.8809ms
  
  2020-09-22 18:24:24.9553|1|INFO|Microsoft.AspNetCore.Routing.EndpointMiddleware|
  Executed endpoint 'MyProject.API.Controllers.MyController.Get (MyProject.API)'
  
  2020-09-22 18:24:24.9553|37|TRACE|Microsoft.AspNetCore.Server.Kestrel|
  Connection id &quot;0HM2V3CUT1GMK&quot; sending HEADERS frame for stream 
  ID 1 with length 53 and flags END_STREAM, END_HEADERS
  
  2020-09-22 18:24:24.9553|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|
  Request finished in 124.9982ms 204
  </code></pre>
  <p>What I don't understand is why ASP.NET Core <code>ControllerActionInvoker</code> and Kestrel take so much time to return the response back! There's nothing custom in between, it's just native code. Any suggestions?</p>
  <p>As side note, I have already implemented a warmup task, which loads all dependencies on application startup. I also have stripped unnecessary filters, input and output formatters and value providers, so the above results are the best I have achieved so far. Why though ASP.NET Core is so slow between returning the response from the controller, to writing the response to I/O and let it fly over the network?</p>
  <p>Do I have to configure something in Kestrel? Do I have to improve something in MVC configuration?</p>
  <p>Finally, as a side note, the code above is optimized, running on release build.</p>