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

πŸ“š .NET Interview Questions

1325 questions for senior .NET engineer interviews, each with a 30-second answer, the full answer, a deep dive, the follow-ups interviewers ask next, and real-world production problems.

Advanced DI

Open generic, conditional, named and keyed registrations, decorators, factory delegates, options with DI, circular dependencies, ValidateScopes and ValidateOnBuild, and DI with Native AOT.

  1. How would you decorate every IRepository<T> with a caching decorator using the built-in container only, without Scrutor?

    Decorating an open generic service such as IRepository<T> in Microsoft.Extensions.DependencyInjection without Scrutor, by closing the types yourself, and the performance and AOT trade-offs.

    Mid-Level Theory Advanced DI
  2. How does the built-in .NET container handle open generic registrations such as AddScoped(typeof(IValidator<>), typeof(Validator<>))? Explain when the type gets closed, how a closed registration overrides the open one, what happens with generic constraints and IEnumerable<T>, and what you cannot do with open generics in Microsoft.Extensions.DependencyInjection.

    How Microsoft.Extensions.DependencyInjection closes open generic registrations at resolution time, closed-over-open precedence, constraint handling, IEnumerable mixing, per-closed-type lifetimes and the limits (no factories, no partial closing, skipped by ValidateOnBuild).

    Mid-Level Theory Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  3. Show how you would use keyed services in .NET 10 to inject two differently configured implementations of the same interface, for example blob storage for invoices and for user uploads. Cover AddKeyedSingleton, [FromKeyedServices], IKeyedServiceProvider, [ServiceKey], KeyedService.AnyKey and what changed in .NET 10, and explain when a factory or strategy resolver is the better choice.

    Keyed services in Microsoft.Extensions.DependencyInjection (.NET 8+, with .NET 10 changes): registering, injecting with [FromKeyedServices] in constructors and Minimal APIs, resolving through IKeyedServiceProvider, [ServiceKey], AnyKey fallbacks, and keyed services versus factories.

    Mid-Level Coding Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  4. Your app needs different implementations or settings depending on a name, the environment or configuration: two HttpClients for different partners, two sets of options for the same class, and a fake email sender in Development. How do you do named and conditional registration in .NET? Cover named and typed HttpClients, named options, registering by environment or configuration, and when the choice has to move to run time.

    Named and conditional registration in .NET: named and typed HttpClients through IHttpClientFactory, named options with IOptionsMonitor.Get and IOptionsSnapshot.Get, registering implementations by environment or configuration in the composition root, and moving decisions to run time with feature flags or resolvers.

    Intermediate Theory Advanced DI Β· πŸ”— 2 follow-ups
  5. How do you register decorators with the .NET dependency injection container? Explain the manual factory approach with the built-in container, how decorators interact with lifetimes and disposal, decoration order, decorating open generics, and when you would use Scrutor's Decorate instead.

    Registering decorators in Microsoft.Extensions.DependencyInjection: the manual inner-concrete-plus-factory approach, lifetime alignment and captive decorators, who disposes inner and outer objects, ordering of stacked decorators, open generic decoration and Scrutor's Decorate/TryDecorate.

    Senior Theory Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  6. Write a factory for objects that need both injected services and run-time arguments, for example a report exporter created per request with a format and a tenant id. Show a registered Func<string, T>, a typed factory interface, and ActivatorUtilities.CreateFactory, and explain who owns and disposes the objects the factory creates.

    Building factories for objects that mix DI dependencies and run-time arguments: Func<string, T> registrations, typed factory interfaces, ActivatorUtilities.CreateInstance versus a cached ObjectFactory from CreateFactory, and the disposal and lifetime rules for objects the container does not track.

    Intermediate Coding Advanced DI Β· πŸ”— 3 follow-ups
  7. How do options work together with dependency injection? Explain IConfigureOptions<T>, IPostConfigureOptions<T> and the order they run in, how to configure options that depend on other services, which lifetimes those configure classes are resolved with, and why calling BuildServiceProvider to get a service inside Configure is wrong.

    The options pipeline as a DI feature: IConfigureOptions, IConfigureNamedOptions, IPostConfigureOptions and IValidateOptions, their execution order in OptionsFactory, configuring options from other services with AddOptions<T>().Configure<TDep> or ConfigureOptions<T>, lifetime rules and the BuildServiceProvider anti-pattern.

    Mid-Level Theory Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  8. After a refactor, your API fails on the first request (or at start-up) with "A circular dependency was detected for the service of type 'OrderService'. OrderService -> InvoiceService -> OrderService." How do you find the cycle, why does it happen, and how do you break it properly? Why is injecting Lazy<T> or IServiceProvider to get around it a smell?

    Diagnosing and fixing circular dependencies in Microsoft.Extensions.DependencyInjection: reading the call-site chain in the exception, why constructor injection can't satisfy a cycle, redesign options (extract a shared service, events, passing data as arguments, merging), and why Lazy<T> and service-locator workarounds hide the design problem.

    Mid-Level Debugging Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  9. What do ValidateScopes and ValidateOnBuild check in the .NET service provider? Explain the exact errors each one raises, why the host turns them on only in Development by default, whether you should turn them on everywhere, and what kinds of DI mistakes they still can't catch.

    ServiceProviderOptions.ValidateScopes and ValidateOnBuild: what each validates and when (resolution versus build), the error messages, the Development-only default in the Generic Host, enabling them in tests or production with UseDefaultServiceProvider, their cost and their blind spots (factories, open generics, service locator calls, root-resolved transients).

    Mid-Level Theory Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  10. You are setting up DI for a large modular .NET 10 solution with ten feature modules and shared infrastructure. How do you organise the registrations? Discuss per-module extension methods, assembly scanning versus explicit registration, ordering and TryAdd for shared services, keeping implementations internal, start-up cost and Native AOT, and how you test the composition root.

    Architecting DI registrations in a modular monolith or large solution: one AddXModule extension per module, internal implementations behind public registration methods, TryAdd for shared infrastructure, explicit versus scanned registration (Scrutor) and its start-up and AOT cost, and testing the composition root with validation and architecture tests.

    Senior Architecture Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world
  11. What does dependency injection cost at run time in .NET, and how does Microsoft.Extensions.DependencyInjection behave under Native AOT and trimming? Explain how the container resolves services fast after the first call, where start-up cost comes from, which registration styles produce trimming warnings, what source-generated containers offer, and when any of this actually matters.

    Performance and Native AOT for the built-in DI container: call-site caching, the switch from the reflection-based runtime resolver to compiled resolvers under JIT, the interpreter used under Native AOT, start-up costs (registration volume, scanning, ValidateOnBuild), trimming annotations and warnings, value-type generic limits, source-generated containers and when DI cost matters.

    Senior Theory Advanced DI Β· πŸ”— 3 follow-ups Β· πŸ›  1 real-world

🌐 Sangam's Webring

βͺ Previous 🏠 Main Next ⏩
πŸ’¬ Chat with me