# Revision: Cheat Sheet and Self-Check — .NET Core: Build, Test and Ship Web APIs

Source: https://www.geekswithgeeks.com/en/dotnet-core/ship-revision

> Review the commands, concepts and common exam questions from the whole course.

## Cheat sheet

**CLI**: `new`, `run`, `build`, `test`, `publish`. **Pipeline**: middleware order matters. **DI**: transient, scoped (per request), singleton. **EF Core**: `DbContext`, migrations, `AsNoTracking`, avoid N+1. **Security**: validate input, `ProblemDetails`, JWT with HTTPS. **Ship**: multi-stage Dockerfile, health checks, measure first.

## Questions interviewers ask

Be ready to explain: the difference between the SDK and runtime, how middleware order affects behaviour, why a singleton must not hold a scoped service, how `async`/`await` frees threads, and how you would find and fix an N+1 query.

**Quiz:** A singleton service needs a DbContext. What is the right fix?

- [ ] Make DbContext a static field
- [x] Inject IServiceScopeFactory or DbContextFactory and create a scope per use
- [ ] Register everything as singleton
- [ ] Disable scope validation

*Answer:* Inject IServiceScopeFactory or DbContextFactory and create a scope per use. Creating a short-lived scope or context per operation avoids the captive dependency and keeps DbContext non-shared.

**Quiz:** Your API returns 500 for bad user input. What is the best change?

- [ ] Catch the exception and return 200
- [ ] Log nothing
- [x] Validate input and return 400 with ProblemDetails
- [ ] Restart the server

*Answer:* Validate input and return 400 with ProblemDetails. Client mistakes are 4xx responses; a 500 should mean a real server fault.

**Quiz:** Which statement about async/await is correct?

- [ ] It creates a new thread for every await
- [x] It lets a thread serve other work while waiting for I/O
- [ ] It makes CPU-bound code faster
- [ ] It must be avoided in web apps

*Answer:* It lets a thread serve other work while waiting for I/O. `await` releases the thread during I/O waits. It does not speed up CPU-bound work.
