API returns status 200 but empty body after IIS restart or Idle time

Binu Thomas 171 Reputation points
2025-02-09T06:36:09.7666667+00:00

I have a .Net core 8 webapi project. it works fine normally. but when it is called after a long idle time or after the iisrestart, returns 200 and the response body is empty.
I tried to log the data, and there is data in the log. don't know what is happening after the return.
did below for app pool
Start Mode to AlwaysRunning.
Idle Time-out to 0

Internet Information Services
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,790 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
387 questions
{count} votes

Accepted answer
  1. MohaNed Ghawar 155 Reputation points
    2025-02-09T10:24:18.9833333+00:00
    1. First, ensure proper startup configuration by adding Application Initialization in your web.config:
         <system.webServer>
             <applicationInitialization>
                 <add initializationPage="/" hostName="localhost" />
             </applicationInitialization>
         </system.webServer>
      
    2. 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;
        });
    
    
    1. Add response compression and configure it:
         builder.Services.AddResponseCompression(options =>
         {
             options.EnableForHttps = true;
         });
         // In Configure method or middleware pipeline
         app.UseResponseCompression();
      
    2. 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>();
      
    3. 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");
             }
         }
      
    4. 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"
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.