Service
“What is a service in ASP.NET Core?”
An object whose job is to do work for other code, handed to the code that needs it.
See it
In plain words
A service is an object whose job is to do work for other code: find a loan, round an , tell the time. The controller does not do that work itself. It asks a service. A is not a service; it is data the services pass around. Some services are written by your team, others come with .NET. You never build them with : the builds each one and hands it to the class that asks for it.
- In ASP.NET Core a service is any type registered in the that the built-in can create and inject, usually behind an and with a of transient, or singleton.
- Classes receive it through injection instead of creating it, which keeps them loosely coupled and testable.
In the lending API
Five files from the lending API, before the database is added. Press Next line to walk through them.
Lending.sln · where these 5 files sit
- src/
- Api/
- Controllers/Servicing/
- Controllers/Servicing/
- Application/Servicing/Loans/
- Infrastructure/Servicing/
- Api/
What they ask next
Interviewers check that you can explain every line. Tap a question to open it.
Why not…Why not write
new LoanService(...)in the controller?Then the controller must also build the repository, the rules and the clock, and it is welded to . A test cannot pass a fake, and adding the database would mean editing every class that calls . The builds it instead, from the in Program.cs.
How does the container know what to build?
From the registrations: "for , build ". It reads 's constructor, builds each thing it asks for first, then builds it. More on the page.
Explain this lineWhy does the controller ask for , not ?
It depends only on the contract. A unit test can pass a fake , and the class behind it can change without the controller knowing. See .
Which services does a real API use?
Written by your team: , , . Comes with .NET: writes logs, reads settings, makes HTTP clients, keeps data in memory, and tells the time. Some need one line first, such as
AddMemoryCache()or . is different: a base class for jobs the app runs, not something you inject.Is every class a service?
No. , and are : code creates them with and passes them around. A service is an object that does work and is handed to you.
What is the difference between a controller, a service and a repository?
The controller speaks HTTP: it reads the request and returns a status code. The service holds the business work, like rounding the . The repository stores and loads data. Each can change without the others.
Should a service keep data in fields?
Not data about one request. A shared service is used by many requests at once, so it should be . The in-memory repository is the exception on purpose: storing loans is its job, so it uses a thread-safe .
Why is the repository a singleton but scoped?
The in-memory repository must be one object, or each request would see an empty store. is scoped because the database repository that replaces it will be scoped, and a service must not outlive what it uses. Lifetimes get their own card; has the basics.
Traps
- Don't say
A service is a web service or a microservice.SayIn dependency injection, a service is any object registered with the container and handed to code that needs it. - Don't say
A service is a class with "Service" in its name.Say, the repository and are services too. What makes it a service is that it is registered and injected.
Infosys (2024) and EPAM Pune (Feb 2026) reports still ask DI in .NET rounds, and reviewing AI-written code is a growing interview task. An AI tool may write new InMemoryLoanRepository() inside a service. It compiles and works, so the catch is yours to explain: the class is now welded to one store and cannot be tested with a fake.