For light, in process work, implement a BackgroundService, create a dependency injection scope for each unit of work so scoped services such as a DbContext behave, and honour the cancellation token for graceful shutdown. Feed it through a bounded channel if requests produce the work. For anything that must survive restarts, run on a schedule, or scale independently, use a durable queue with a separate worker, or a job library with persistent storage.
Why interviewers ask this
Background work in web apps fails in quiet ways: jobs lost on restart, scoped services used from a singleton, unhandled exceptions stopping the host, and work duplicated across instances. The interviewer wants to see that you know the hosted service model, handle scopes and cancellation properly, and recognise when in process work is the wrong tool. Thinking about multiple instances shows you have run this in production rather than only locally.
How to structure your answer
- Describe hosted services and BackgroundService.
- Cover scopes, cancellation and exception handling.
- Explain handing work over from requests through a channel.
- Say when to move to a durable queue or separate worker.
Example answer
For simple in process work I write a BackgroundService and register it as a hosted service. Three details matter. Hosted services are singletons, so for each unit of work I create a scope from the scope factory and resolve the DbContext inside it. I pass the stopping token through every async call, so shutdown is clean instead of dying mid write. And I wrap each iteration in a try catch with logging, because an unhandled exception stops the service, and in recent versions it stops the whole host by default. If requests need to hand off work, like sending a confirmation email, I use a bounded channel, so the endpoint returns quickly and back pressure kicks in if the worker falls behind. The limits matter most. Anything in memory is lost on a restart or deployment, and with three instances behind a load balancer, a timer based job runs three times. So for work that must not be lost, or must run exactly once on a schedule, I use a durable queue with a separate worker, or a job library backed by a database.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- How would you make sure a scheduled job runs only once across several instances?
- What happens to in flight work when the app is shutting down?
- When would you split background processing into its own service?
Related .net developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions