[Intermediate] ·
WebApplicationFactory, Testcontainers, and getting an ASP.NET Core service into production sensibly.
Integration testing is unusually good here
WebApplicationFactory<T> spins up your entire application in-process — real pipeline, real DI, real routing — and gives you an HttpClient that talks to it without a network.
public class OrdersApiTests(WebApplicationFactory<Program> factory)
: IClassFixture<WebApplicationFactory<Program>>
{
[Fact]
public async Task Get_ReturnsNotFound_ForMissingOrder()
{
var client = factory.CreateClient();
var response = await client.GetAsync("/orders/999999");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}This is one of the best testing stories in any web ecosystem. You test the actual middleware order, actual model binding, actual serialisation and actual auth — the things unit tests structurally cannot cover → note 01.
Override dependencies for a test:
factory.WithWebHostBuilder(b => b.ConfigureServices(services =>
{
services.RemoveAll<IEmailSender>();
services.AddSingleton<IEmailSender, FakeEmailSender>();
}));Test against a real database
Testcontainers starts a real Postgres in Docker for the test run:
var db = new PostgreSqlBuilder().WithImage("postgres:16").Build();
await db.StartAsync();
// point the app at db.GetConnectionString()This replaced the EF In-Memory provider, which was a persistent source of false confidence — it doesn’t enforce constraints, doesn’t do real SQL translation, and passes tests that fail against an actual database. The EF team now recommends against it for testing.
The general rule from V&V: test in an environment that resembles the one that matters. A fake database is a different environment.
Observability
builder.Services.AddOpenTelemetry()
.WithTracing(t => t.AddAspNetCoreInstrumentation().AddEntityFrameworkCoreInstrumentation())
.WithMetrics(m => m.AddAspNetCoreInstrumentation().AddRuntimeInstrumentation());OpenTelemetry is the default answer, and .NET’s built-in instrumentation is genuinely good — Activity (spans) and Meter are in the BCL, not a library → observability.
Structured logging, not string interpolation:
logger.LogInformation("Order {OrderId} created for {CustomerId}", id, customerId); // ✓
logger.LogInformation($"Order {id} created"); // ✗The first preserves OrderId as a queryable field. The second produces a string you can only grep, and it formats even when the level is disabled → the same rule in Python.
Health checks are built in:
builder.Services.AddHealthChecks().AddDbContextCheck<AppDb>();
app.MapHealthChecks("/health/ready");
app.MapHealthChecks("/health/live", new() { Predicate = _ => false });Liveness and readiness are different questions. Liveness: is the process alive? — restart if not. Readiness: can it serve traffic? — remove from the load balancer if not. Wiring a database check into liveness means a brief database blip restarts every pod, which turns a small problem into an outage → orchestration.
Deployment
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore # cached layer — restore before copying source
COPY . .
RUN dotnet publish -c Release -o /app
FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .
USER $APP_UID # don't run as root
ENTRYPOINT ["dotnet", "MyApi.dll"]Restore before copying the full source so dependency restore is cached → multi-stage builds.
Runtime image, not SDK — the SDK image is several times larger and ships a compiler into production.
Native AOT is worth considering for serverless and short-lived workloads — millisecond startup, small footprint — at the cost of reflection-based libraries → note 13. For a long-running API, stay with JIT; the startup cost amortises to nothing and you keep the runtime optimisations.
Configuration and secrets in production
Environment variables override appsettings.json, and the double-underscore convention maps to nesting:
ConnectionStrings__Db=Host=...;Password=...
Secrets belong in a secret store, injected as env vars or fetched at startup — never in the image, never in the repo → secret management.
ASPNETCORE_ENVIRONMENT drives which appsettings.{env}.json loads and whether the developer exception page is on. Getting this wrong in production leaks stack traces to users.
The production checklist
- HTTPS and HSTS
ForwardedHeadersif behind a proxy, or every client IP is the proxy’s- Rate limiting — built in since .NET 7 (
AddRateLimiter) - CORS, configured deliberately rather than
AllowAnyOrigin ProblemDetailsfor errors; no stack traces to clients- Health checks split liveness/readiness
- Graceful shutdown — ASP.NET Core handles SIGTERM; make sure background work honours the cancellation token
dotnet list package --vulnerablein CI → DevSecOps
Related
- data access
- C#: testing and tooling — the unit-test layer
- observability · CI/CD
Source: [reference] — from the ASP.NET Core documentation, Aug 2026.
Software engineering
Infrastructure & security
Computer science
Hardware & engineering