I had answered a similar question related to benchmarking performance for ASP.NET Core Kestrel here.
In short, you could remove the middleware that is causing the bottlenecks, or at least this could help you diagnose the culprit.
Ordering
The order that middleware components are added in the Configure
method defines the order in which they are invoked on requests, and the reverse order for the response. This ordering is critical for security, performance, and functionality.
------ Source: Microsoft Docs: ASP.NET Core Middleware
Short-Circuiting
You can short-circuit the request pipeline to handle performance optimizations (or testing) by not calling the next
parameter. For example:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// make sure you place this at the top
// the request pipeline will go in sequence
app.Use(async (context, next) =>
{
// do work for your special case, performance tests, etc
// in order to short-circuit the pipeline, do NOT call the next parameter
// so, you could place some kind of conditional here that will allow only
// specific requests to continue down/up the pipeline
if (!true)
{
await next.Invoke();
}
});
// the rest of the pipeline
}
}