📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

What does ValidateOnBuild check, and what can it not catch?

Intermediate Theory Intermediate aspnetcore dependency-injection service-provider-options validate-on-build validate-scopes

🎯 Key points

  • ValidateOnBuild: at Build(), tries to create a call site for every non-open-generic registration
  • Catches missing constructor dependencies and unresolvable constructors before the first request
  • With ValidateScopes, also catches singletons that depend on scoped services
  • Cannot see inside factory delegates (sp => ...): they are opaque code run later
  • Cannot check open generics until closed, or types made with ActivatorUtilities / GetService at run time
  • WebApplication turns both on in the Development environment only; set them in UseDefaultServiceProvider for all environments
  • Cost is a one-off start-up walk of the graph, usually tens of milliseconds

⏱ 30-second answer

ValidateOnBuild makes Build() walk every registration and try to build its call site, so a missing dependency or an unresolvable constructor fails at start-up instead of on the first request that needs it. Combined with ValidateScopes it also catches a singleton depending on a scoped service.

What it cannot catch:

  • anything inside a factory delegate (sp => ...): the body is opaque code that only runs on resolution;
  • open generics, which can't be checked until a closed type is requested;
  • types created later with ActivatorUtilities.CreateInstance or GetService calls in your own code.

ASP.NET Core's WebApplication turns it on only in the Development environment; enable it everywhere with builder.Host.UseDefaultServiceProvider(o => { o.ValidateOnBuild = true; o.ValidateScopes = true; }). The start-up cost is a one-off graph walk, typically small, and it buys you a fast failure in every environment.

🔍 What the interviewer is checking

Checks the limits of container validation

↩ Usually asked after

More in DI Core Concepts

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me