ASP.NET Core Web API Interview Question #175

What are best practices for testing, outbound HTTP, background work, and Web API deployment?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Use unit tests for focused business behavior and integration tests for the real HTTP pipeline. Test authorization boundaries, validation, response contracts, and important persistence behavior using infrastructure realistic enough for the risk involved. Use IHttpClientFactory or typed clients for outbound HTTP and handle timeouts, cancellation, and failures deliberately.

Detailed Explanation

Use unit tests for focused business behavior and integration tests for the real HTTP pipeline. Test authorization boundaries, validation, response contracts, and important persistence behavior using infrastructure realistic enough for the risk involved. Use IHttpClientFactory or typed clients for outbound HTTP and handle timeouts, cancellation, and failures deliberately.

Move durable or long-running work outside normal request execution, use 202 Accepted for appropriate asynchronous workflows, and make hosted services respect scopes and shutdown. Keep deployment configuration externalized, protect secrets, apply migrations through a controlled process, design for rolling compatibility, and monitor logs, metrics, traces, dependencies, and business outcomes after release.

Code Example

builder.Services.AddHttpClient<ICatalogClient, CatalogClient>();
builder.Services.AddHostedService<CleanupWorker>();
builder.Services.AddHealthChecks();

app.MapHealthChecks("/health");
app.MapControllers();

// CI/CD:
// test -> contract check -> migrate -> deploy -> monitor