← Foundations path
.NETFoundations 21 / 37

Registering services

“How do you register a service in ASP.NET Core, and what happens if you forget one?”

The lines in Program.cs that tell the container: when someone asks for this, build that.

See it

When asked forthe needbuildthis classthe list Program.cs writesTimeProviderTimeProvider.SystemICountryRulesIndiaRulesILoanRepositoryInMemoryLoanRepositoryILoanServiceLoanServiceneeds ILoanServicenew LoanService(…)ValidateOnBuildapp starts500: Unable to resolve ILoanRepositoryICountryRulesFakeRulessingle ask gets FakeRules
1 / 10Each registration line adds one row: when someone asks for , build .

In plain words

Program.cs holds a list of lines like AddScoped<ILoanService, LoanService>(). Each one means: when someone asks for , build a . Writing the list builds nothing; the builds objects later, when they are . Forget a line and the code still compiles, but the app fails when that object is needed. The word in the method name (Singleton, Scoped, Transient) says how long the object lives; that is .

Say it in the interview

In the lending API

Program.cs before and after the fix. Both were run with .NET 10 and give the errors shown. Press Next line to walk through them.

Lending.sln · where these 2 files sit
  • src/Api/
Diagram · step 1Each registration line adds one row: when someone asks for , build .
When asked forthe needbuildthis classthe list Program.cs writesTimeProviderTimeProvider.SystemICountryRulesIndiaRulesILoanRepositoryInMemoryLoanRepositoryILoanServiceLoanServiceneeds ILoanServicenew LoanService(…)ValidateOnBuildapp starts500: Unable to resolve ILoanRepositoryICountryRulesFakeRulessingle ask gets FakeRules
Program.old.cs · line 10 · 1 of 6builder.Services is the list. Framework features register many rows in one call: adds everything MVC (the controller framework) needs.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

With AI tools

An AI tool often adds a new constructor parameter to a service and forgets the matching line in Program.cs. It compiles and the unit tests pass, because they never touch the container. Interviews now include reviewing AI-written code, and "where is this registered?" is the catch. makes that mistake fail at startup.

Read more: Microsoft Learn: DI in ASP.NET Core · Microsoft Learn: .NET dependency injection · ServiceProviderOptions.ValidateOnBuild