Hosted Services vs Windows Services in .NET Core

πŸ’‘ Concept Name

Hosted Services vs Windows Services

πŸ“˜ Quick Intro

Both Hosted Services and Windows Services are used to run background tasks in .NET Core. But they differ in how they are hosted, managed, and deployed.

🧠 Analogy / Short Story

Think of a Windows Service like an always-running machine in a factory. It starts with the factory (Windows) and runs behind the scenes. A Hosted Service, on the other hand, is like a helper robot inside an app that works only when the app runsβ€”it’s easier to build and test inside the same environment.

πŸ”§ Technical Explanation

  • Windows Service: A background process registered with Windows OS, runs independently of web apps.
  • Hosted Service: Implemented via IHostedService or BackgroundService inside a .NET Core Host, typically part of an ASP.NET Core app or Worker.
  • Registration: Windows Services need to be installed; Hosted Services run inside the app’s host pipeline.

🎯 Purpose & Use Case

  • βœ… Use Hosted Services for background jobs like polling APIs, queue processing inside web or worker apps.
  • βœ… Use Windows Services for system-level services like antivirus, logging daemons, or daemon-type apps on Windows.

πŸ’» Real Code Example

// Implement a Hosted Service
public class MyBackgroundService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            Console.WriteLine(""Working..."");
            await Task.Delay(1000, stoppingToken);
        }
    }
}

// Register in Program.cs
builder.Services.AddHostedService<MyBackgroundService>();

❓ Interview Q&A

Q1: What is a Hosted Service?
A: A background task running in a .NET Core Host using IHostedService.

Q2: What is a Windows Service?
A: A system-level service managed by Windows OS.

Q3: How are Hosted Services registered?
A: Using AddHostedService<T>() in Program.cs.

Q4: Can Hosted Services run without the app running?
A: No, they are tied to the application's host.

Q5: Can you run a Worker Service as a Windows Service?
A: Yes, by adding .UseWindowsService() in the Host builder.

Q6: What interface defines a Hosted Service?
A: IHostedService.

Q7: Which service type is easier to debug and test?
A: Hosted Services.

Q8: Which runs as a standalone OS process?
A: Windows Service.

Q9: What lifecycle methods must a Hosted Service implement?
A: StartAsync and StopAsync.

Q10: Where should you use Hosted Services?
A: In ASP.NET Core or Worker Service apps needing background jobs.

πŸ“ MCQs

Q1. Which interface is used to create a Hosted Service?

  • IWindowsService
  • IService
  • IHostedService
  • IBackgroundWorker

Q2. Where do Hosted Services run?

  • As system services
  • In browser
  • Inside the app's Host
  • As scheduled tasks

Q3. How do you register a Hosted Service?

  • AddService&lt;T&gt;()
  • RegisterWorker&lt;T&gt;()
  • AddHostedService&lt;T&gt;()
  • UseWindowsService()

Q4. Which method must be overridden in BackgroundService?

  • StartService
  • Run
  • ExecuteAsync
  • Launch

Q5. Windows Services are tied to?

  • .NET Core Host
  • IServiceCollection
  • Operating System
  • Web Server

Q6. Can Hosted Services be debugged like regular .NET apps?

  • No
  • Only in production
  • Yes
  • Only on Windows

Q7. Which service type starts with OS boot?

  • Hosted Service
  • ASP.NET App
  • Windows Service
  • Worker Process

Q8. What does UseWindowsService() do?

  • Creates a web server
  • Starts DI container
  • Configures the Host to run as a Windows Service
  • Creates console logs

Q9. Hosted Service ends when?

  • After 1 hour
  • System sleeps
  • App shuts down
  • Never

Q10. Which service is more portable?

  • Windows Service
  • Hosted Service
  • WCF
  • None

πŸ’‘ Bonus Insight

You can use a .NET Core Worker Service template to create a cross-platform background task that can be run as a Windows Service or a Linux daemon with minimal configuration.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: