- First, ensure proper startup configuration by adding Application Initialization in your web.config:
<system.webServer> <applicationInitialization> <add initializationPage="/" hostName="localhost" /> </applicationInitialization> </system.webServer>
- In your Program.cs or Startup.cs, make sure JSON serialization is properly configured:
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
- Add response compression and configure it:
builder.Services.AddResponseCompression(options => { options.EnableForHttps = true; }); // In Configure method or middleware pipeline app.UseResponseCompression();
- Implement a warmup middleware:
public class WarmupMiddleware { private readonly RequestDelegate _next; public WarmupMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { // Ensure JsonSerializer is initialized JsonSerializer.Serialize(new { }); await _next(context); } } // Register in Program.cs app.UseMiddleware<WarmupMiddleware>();
- Check if you're using async/await correctly in your controllers:
[ApiController] [Route("[controller]")] public class YourController : ControllerBase { [HttpGet] public async Task<ActionResult<YourModel>> Get() { var result = await _service.GetDataAsync(); // Explicitly serialize to ensure proper response var json = JsonSerializer.Serialize(result); return Content(json, "application/json"); } }
- Configure IIS Application Pool advanced settings:
- Set "Idle Time-out Action" to "Suspend"
- Enable "Preload Enabled"
- Set "Start Mode" to "AlwaysRunning"
- Configure IIS Application Pool advanced settings:
- Set "Idle Time-out Action" to "Suspend"
- Enable "Preload Enabled"
- Set "Start Mode" to "AlwaysRunning"