ASP.NET Core Web API Interview

Testing, HttpClient, Integration Testing, Background Services & Deployment

ASP.NET Core Web API interview questions covering testing, HttpClient, integration testing, background services and deployment.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
151
SeniorAdvanced

What is unit testing in ASP.NET Core Web API?

Unit testing verifies a small unit of application behavior in isolation, such as a service method, validator, mapper, or controller decision. Dependencies are normally replaced with test doubles so the test focuses on one behavior.

152
SeniorAdvanced

What should be unit tested in a Web API?

Unit tests are most valuable around business rules, application services, validation logic, authorization decisions, transformations, and controller behavior that contains meaningful branching.

153
SeniorAdvanced

What is integration testing in ASP.NET Core Web API?

Integration testing verifies multiple application components working together through realistic infrastructure boundaries. In ASP.NET Core, integration tests commonly start the application in a test host and send real HTTP requests through the middleware, routing, authentication, model binding, and endpoint pipeline.

154
SeniorAdvanced

What is WebApplicationFactory in ASP.NET Core testing?

WebApplicationFactory<TEntryPoint> creates a test server for an ASP.NET Core application and provides HttpClient instances that call the application in memory. It is commonly used for end-to-end integration tests of the HTTP pipeline.

155
SeniorAdvanced

How can dependencies be replaced in integration tests?

WebApplicationFactory can customize the service collection for tests. A production dependency can be removed and replaced with a test implementation, mock, fake, or test database configuration.

156
SeniorAdvanced

Should Web API tests use an in-memory database?

An in-memory database can make some tests fast, but it may not reproduce the behavior of the production database provider. Differences in SQL translation, constraints, transactions, collation, and provider-specific features can hide real defects.

157
SeniorAdvanced

How should authentication be tested in API integration tests?

Tests should cover both authenticated and unauthenticated behavior and verify important authorization boundaries. A test authentication scheme can create controlled identities and claims without requiring the real external identity provider.

158
SeniorAdvanced

What API response behaviors should integration tests verify?

Verify status codes, response bodies, content types, headers, validation errors, authentication and authorization outcomes, Location headers for created resources, and important side effects.

159
SeniorAdvanced

What is contract testing for APIs?

Contract testing verifies that an API provider and its consumers agree on request and response expectations. It is particularly useful when services are deployed independently and a provider change could break downstream clients.

160
SeniorAdvanced

How can OpenAPI be used in automated API testing?

The generated OpenAPI document can be validated in CI, compared for unexpected breaking changes, or used to generate clients and test requests. This helps ensure documentation and implementation remain aligned.

161
SeniorAdvanced

What is IHttpClientFactory?

IHttpClientFactory creates configured HttpClient instances while managing the lifetime of underlying message handlers. It integrates HTTP clients with dependency injection, logging, configuration, and resilience features.

162
SeniorAdvanced

What is a typed HttpClient?

A typed HttpClient wraps HttpClient inside a dedicated service representing one external API. The class can expose domain-specific methods and keep URLs, serialization, headers, and error handling out of controllers.

163
SeniorAdvanced

How should an API handle outbound HTTP failures?

Distinguish cancellation, timeout, transient transport failure, expected downstream 4xx responses, and server-side 5xx responses. Do not convert every failure into an identical exception or retry every status automatically.

164
SeniorAdvanced

What is a BackgroundService in ASP.NET Core?

BackgroundService is a base class for long-running hosted work managed by the application's host. A derived class implements ExecuteAsync and observes the supplied stopping token.

165
SeniorAdvanced

What is IHostedService?

IHostedService defines StartAsync and StopAsync for services whose lifecycle is controlled by the ASP.NET Core host. BackgroundService is a convenience implementation for services centered around a long-running ExecuteAsync loop.

166
SeniorAdvanced

How should a hosted service use scoped dependencies such as DbContext?

Hosted services are normally registered as singletons, so they should not directly capture a scoped DbContext. Instead, inject IServiceScopeFactory, create a scope for each unit of work, and resolve scoped services from that scope.

167
SeniorAdvanced

Why should long-running work not normally execute inside an HTTP request?

Long-running work keeps the request open, consumes resources, increases timeout risk, and couples client connectivity to the completion of the job. For lengthy or retryable operations, the API can accept the request, enqueue durable work, and return a job identifier.

168
SeniorAdvanced

What does HTTP 202 Accepted mean?

202 Accepted means the request has been accepted for processing but the processing has not yet completed. It is suitable for asynchronous workflows where work continues after the HTTP response.

169
SeniorAdvanced

What is graceful shutdown in ASP.NET Core?

Graceful shutdown gives the application an opportunity to stop accepting work and allow in-flight operations or hosted services to finish or cancel safely before the process exits.

170
SeniorAdvanced

What is the role of environment-specific configuration during deployment?

ASP.NET Core can load configuration from appsettings files, environment variables, secret stores, command-line values, and other providers. Deployment environments should supply values such as connection strings, service endpoints, logging levels, and feature settings without requiring source-code changes.

171
SeniorAdvanced

What are ASP.NET Core environments?

ASP.NET Core commonly distinguishes Development, Staging, and Production environments, although custom names are possible. Environment-specific behavior can control diagnostics, middleware, configuration, and development tooling.

172
SeniorAdvanced

How should database migrations be handled during API deployment?

Database migrations should be applied through a controlled deployment process appropriate to the system's scale and availability requirements. Automatically migrating at application startup can be convenient for small systems but can create race conditions or risky schema changes in scaled production environments.

173
SeniorAdvanced

What is a rolling or zero-downtime deployment concern for APIs?

During a rolling deployment, old and new application versions may run simultaneously. Database schema, message formats, cache entries, and external contracts should therefore remain compatible across the transition.

174
SeniorAdvanced

What should be monitored after deploying a Web API?

Monitor request rate, latency, error rate, status-code distribution, dependency performance, resource utilization, database behavior, health checks, and important business signals. Logs, metrics, and distributed traces provide complementary views of production behavior.

175
SeniorAdvanced

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

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.