I have a container with .Net Host and a BackgroundService running in it. It works fine on my mac. When deployed to Azure App Service on Linux, it starts then terminates immediately. It seems to me that Azure App Service on Linux is expecting the container to respond to HTTP pings, but my container is a background service, not a web server, and doesn't expose any ports. How can I get my dockerized .Net Host with BackgroundService to run smoothly in an Azure App Service for Linux?
Should I convert it to a WebHost just to get it to respond to pings? Should I be using some other type of Azure infrastructure (Container Apps?) Can I turn off the HTTP pings? (No obvious way in the portal to do so) Any other thoughts?
Logs look like this.
2024-04-26T19:45:41.663Z INFO - Starting container for site
2024-04-26T19:45:41.663Z INFO - docker run -d -p 4665:8080 --name redactedappname1302_0_61646b2f -e WEBSITE_USE_DIAGNOSTIC_SERVER=false -e WEBSITES_PORT=8080 -e WEBSITE_SITE_NAME=redactedappname1302 -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=redactedappname1302.azurewebsites.net -e WEBSITE_INSTANCE_ID=2c847a802c8040b33d7df73fac3a50614d8069754a88be4922fa1677ca17ce3d -e HTTP_LOGGING_ENABLED=1 redactedregistryname.azurecr.io/redactedappname:20240426.20.2cf015ff -p 80:8080
2024-04-26T19:45:44.387Z INFO - Initiating warmup request to container redactedappname1302_0_61646b2f for site redactedappname1302
2024-04-26T19:45:49.146Z ERROR - Container redactedappname1302_0_61646b2f for site redactedappname1302 has exited, failing site start
2024-04-26T19:45:49.159Z ERROR - Container redactedappname1302_0_61646b2f didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2024-04-26T19:45:49.233Z INFO - Stopping site redactedappname1302 because it failed during startup.
Code (redacted for reasons of IP/employer &C!) looks like this ..
var builder = Host.CreateDefaultBuilder();
builder.ConfigureAppConfiguration((context, configBuilder) =>
{
//redacted, configuring logging, all working locally
});
builder.ConfigureServices((context, services) =>
{
services.
//Add various services, redacted, all working locally
//most importantly, add MyBackgroundService
.AddHostedService<MyBackgroundService>();
});
Log.Information("Starting ...");
var app = builder.Build();
try
{
await app.RunAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Log.Error(ex, "Failure");
}
Should I convert it to a WebHost just to get it to respond to pings? Should I be using some other type of Azure infrastructure (Container Apps?) Can I turn off the HTTP pings? (No obvious way in the portal to do so) Any other thoughts?
Convert your app to a web host to respond to pings. I created a simple app and a background service running in it.
This is my Program.cs :
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHostedService<MyBackgroundService>();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{ options.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
if (app.Environment.IsDevelopment())
options.RoutePrefix = "swagger";
else
options.RoutePrefix = string.Empty;
}
);
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
BackgroundService.cs :
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
public class MyBackgroundService : BackgroundService
{
private readonly ILogger<MyBackgroundService> _logger;
public MyBackgroundService(ILogger<MyBackgroundService> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Background service is running.");
Console.WriteLine("Background service is running.");
await Task.Delay(5000, stoppingToken);
}
}
}
LocalOutput:
Deployed the app via Azure Container Registry:
Add startup command while entering the details of your Azure container registry.
Here's log stream: