Background Tasks in .NET Core

 
The Background on
Background Tasks in .NET Core
 
 
 
 
 
Slides up at scottsauber.com
Audience
 
.NET Core Developers
In need of running a background task
 
Agenda
 
What are background tasks/jobs?
What type of problems are suitable for a background task/job?
What options are out there?
IHostedService
BackgroundService
Worker Service
Hangfire
Why would I choose one over the other?
Deep dive into each
Demos
Questions
 
Goal
 
Know all your options for running background tasks
Why choose one over another
Who am I?
 
 
Software Consultant at Lean TECHniques
Co-organizer of 
Iowa .NET User Group
Friend of Redgate
Blog at 
scottsauber.com
What problem do background tasks solve?
 
Cron jobs
Process messages from a queue every X minutes
Clean up database or file system every X minutes
Send email notification every X minutes under certain circumstances
Refresh cache every X minutes
Check for updates to database every X minutes and push updates via SignalR
Perform some CPU intensive work asynchronously
Re-train ML datasets
 
Options
 
IHostedService
BackgroundService
WorkerService
Hangfire
Cloud options
 
These options are
kind of like
baking cookies
 
IHostedService
“Make Your Own Recipe”
(Cookie Jar Included)
What is an IHostedService?
 
Lets you host a background job inside an ASP.NET Core App
ASP.NET Core app is your cookie jar
Interface with StartAsync and StopAsync
Raw, fundamental building block for other options
Register via dependency injection and services.AddHostedService<T>
 
Demo
How does an IHostedService work?
 
Register with DI
StopAsync’s cancellation token has 5 seconds to shutdown gracefully
StopAsync might not be called if the app shuts down unexpectedly
 
How does an IHostedService work?
 
Image Credit: 
Andrew Lock
How does an IHostedService work?
 
StartAsync blocks the rest of your app from starting
Push 
blocking
 long-running work out of StartAsync
This goes for BackgroundService later
 
 
 
 
 
 
UNLESS, you truly don’t want your app to boot until this finishes
i.e. Database Migrations
When do I use IHostedService?
 
You will implicitly use it with BackgroundService and Worker Services
You need full control over Starting and Stopping
AND will not use the base BackgroundService implementation
When do I 
NOT
 use IHostedService?
 
Should be using BackgroundService or WorkerService 95%+ of the time
Other reasons will be the same as BackgroundService (next)
 
BackgroundService
“Follow The Recipe”
(Cookie Jar Included)
What is a BackgroundService?
 
Lets you host a background job inside an ASP.NET Core App
ASP.NET Core app is your cookie jar
Abstract class, implements IHostedService
Exposes ExecuteAsync abstract method
Handles Starting and Stopping
 
Demo
How does a BackgroundService work?
 
Register with DI
Exposes ExecuteAsync abstract method
Can still override StartAsync and StopAsync
When do I use BackgroundService?
 
Need a simple background task runner
Either as part of your ASP.NET Core application or by itself
Less gotchas than IHostedService
Can’t accidentally prevent app from booting unless override StartAsync
Handles cancellations
Want an ASP.NET Core endpoint to health check your background task
Instead of WorkerServices
When do I 
NOT
 use BackgroundService?
 
Too much co-location with your app/API can get unruly and outweigh
the convenience of co-location
It Depends
Scaling out can be a problem if your code isn’t idempotent
Fix by making code idempotent or not allowing scale out
 
WorkerService
“Follow The Recipe”
(BYO Cookie Jar)
What is a WorkerService?
 
Enhanced .NET Core Console App template
dotnet new worker –o my-custom-worker
Allows you to have an IHost
Configuration, Dependency Injection, Logging, etc.
Registers a Worker class as a HostedService
Does not take an opinion on how to host console app
No cookie jar
Console app called from scheduler
Windows Service
systemd
 
Demo
 
How does a WorkerService work?
 
Project Sdk of Microsoft.NET.Sdk.Worker
PackageReference to Microsoft.Extensions.Hosting
How do I host WorkerServices?
 
Scheduler calls Console App
Windows Scheduled Tasks, k8s cron jobs, Azure Logic Apps, AWS Scheduled
Tasks, GCP Cloud Scheduler
Windows Service or Systemd (Windows or Linux)
When do I use WorkerServices?
 
Want an out-of-proc way of running background tasks
Prefer hosting background services outside of a web app
Avoid app pool recycles
Natural migration for a full .NET framework Windows Service
When do I 
NOT
 use WorkerServices?
 
Prefer deploying as a web app
Want to co-locate with existing web app/API
Want a healthcheck endpoint
 
Hangfire
“Buy pre-packaged cookies”
What is Hangfire?
 
Full featured library for running jobs in ASP.NET Core
Free for commercial use but paid if you want support ($500-$4500/yr)
Comes with UI for monitoring and history
Supports Cron and ad-hoc running of jobs
Allows for continuations
Automatic retries
Supports concurrency limiting
Persists job state to database
 
Demo
How does Hangfire work?
 
Serializes method call and all arguments
Creates background job based on that information
Saves job to persistent storage
Starts background job if immediate
When do I use Hangfire?
 
Want to host jobs in ASP.NET Core
Need features Hangfire offers
Don’t want to write plumbing code
Ok with relying on a 3
rd
 party library
When do I 
NOT
 use Hangfire?
 
Do not want to host jobs in ASP.NET Core
Have basic needs and do not need Hangfire’s features
Do not want to rely on 3
rd
 party library
More control over what happens
 
Cloud options
 
Azure Functions
Azure WebJobs
AWS Lambdas
GCP Cloud Scheduler + Cloud Functions
Didn’t cover these to avoid cloud specific
 
Takeaways
 
Awareness to all the options available to you
More information to make the best decision for you and your company
 
Resources
 
https://docs.microsoft.com/en-
us/dotnet/architecture/microservices/multi-container-microservice-
net-applications/background-tasks-with-ihostedservice
https://www.hangfire.io/
https://app.pluralsight.com/library/courses/building-aspnet-core-
hosted-services-net-core-worker-services/
This slide deck
 
Questions?
 
 
 
 
 
 
Slides up at scottsauber.com
 
Thanks!
 
 
 
 
 
 
Slides up at scottsauber.com
Slide Note

Pre-check:

docker-compose down && docker-compose up

New Database for Hangfire

Solution Open

Embed
Share

Explore the world of background tasks in .NET Core with this informative slideshow by scottsauber. Learn about various types of background tasks, suitable problems, available options like IHostedService, BackgroundService, Worker Service, Hangfire, and more. Discover how to choose the right option for your needs and get insights into running background tasks efficiently. Whether you're a .NET Core developer or looking to delve into running background tasks, this slideshow has got you covered!

  • .NET Core
  • Background Tasks
  • IHostedService
  • Worker Service
  • Hangfire

Uploaded on Sep 23, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. The Background on Background Tasks in .NET Core scottsauber Slides up at scottsauber.com

  2. Audience .NET Core Developers In need of running a background task scottsauber

  3. Agenda What are background tasks/jobs? What type of problems are suitable for a background task/job? What options are out there? IHostedService BackgroundService Worker Service Hangfire Why would I choose one over the other? Deep dive into each Demos Questions scottsauber

  4. Goal Know all your options for running background tasks Why choose one over another scottsauber

  5. Who am I? Software Consultant at Lean TECHniques Co-organizer of Iowa .NET User Group Friend of Redgate Blog at scottsauber.com scottsauber

  6. What problem do background tasks solve? Cron jobs Process messages from a queue every X minutes Clean up database or file system every X minutes Send email notification every X minutes under certain circumstances Refresh cache every X minutes Check for updates to database every X minutes and push updates via SignalR Perform some CPU intensive work asynchronously Re-train ML datasets scottsauber

  7. Options IHostedService BackgroundService WorkerService Hangfire Cloud options scottsauber

  8. These options are kind of like baking cookies

  9. IHostedService Make Your Own Recipe (Cookie Jar Included)

  10. What is an IHostedService? Lets you host a background job inside an ASP.NET Core App ASP.NET Core app is your cookie jar Interface with StartAsync and StopAsync Raw, fundamental building block for other options Register via dependency injection and services.AddHostedService<T> scottsauber

  11. Demo

  12. How does an IHostedService work? Register with DI StopAsync s cancellation token has 5 seconds to shutdown gracefully StopAsync might not be called if the app shuts down unexpectedly scottsauber

  13. How does an IHostedService work? scottsauber Image Credit: Andrew Lock

  14. How does an IHostedService work? StartAsync blocks the rest of your app from starting Push blocking long-running work out of StartAsync This goes for BackgroundService later UNLESS, you truly don t want your app to boot until this finishes i.e. Database Migrations scottsauber

  15. When do I use IHostedService? You will implicitly use it with BackgroundService and Worker Services You need full control over Starting and Stopping AND will not use the base BackgroundService implementation scottsauber

  16. When do I NOT use IHostedService? Should be using BackgroundService or WorkerService 95%+ of the time Other reasons will be the same as BackgroundService (next) scottsauber

  17. BackgroundService Follow The Recipe (Cookie Jar Included)

  18. What is a BackgroundService? Lets you host a background job inside an ASP.NET Core App ASP.NET Core app is your cookie jar Abstract class, implements IHostedService Exposes ExecuteAsync abstract method Handles Starting and Stopping scottsauber

  19. Demo

  20. How does a BackgroundService work? Register with DI Exposes ExecuteAsync abstract method Can still override StartAsync and StopAsync scottsauber

  21. scottsauber

  22. When do I use BackgroundService? Need a simple background task runner Either as part of your ASP.NET Core application or by itself Less gotchas than IHostedService Can t accidentally prevent app from booting unless override StartAsync Handles cancellations Want an ASP.NET Core endpoint to health check your background task Instead of WorkerServices scottsauber

  23. When do I NOT use BackgroundService? Too much co-location with your app/API can get unruly and outweigh the convenience of co-location It Depends Scaling out can be a problem if your code isn t idempotent Fix by making code idempotent or not allowing scale out scottsauber

  24. WorkerService Follow The Recipe (BYO Cookie Jar)

  25. What is a WorkerService? Enhanced .NET Core Console App template dotnet new worker o my-custom-worker Allows you to have an IHost Configuration, Dependency Injection, Logging, etc. Registers a Worker class as a HostedService Does not take an opinion on how to host console app No cookie jar Console app called from scheduler Windows Service systemd scottsauber

  26. Demo

  27. How does a WorkerService work? Project Sdk of Microsoft.NET.Sdk.Worker PackageReference to Microsoft.Extensions.Hosting scottsauber

  28. How do I host WorkerServices? Scheduler calls Console App Windows Scheduled Tasks, k8s cron jobs, Azure Logic Apps, AWS Scheduled Tasks, GCP Cloud Scheduler Windows Service or Systemd (Windows or Linux) scottsauber

  29. When do I use WorkerServices? Want an out-of-proc way of running background tasks Prefer hosting background services outside of a web app Avoid app pool recycles Natural migration for a full .NET framework Windows Service scottsauber

  30. When do I NOT use WorkerServices? Prefer deploying as a web app Want to co-locate with existing web app/API Want a healthcheck endpoint scottsauber

  31. Hangfire Buy pre-packaged cookies

  32. What is Hangfire? Full featured library for running jobs in ASP.NET Core Free for commercial use but paid if you want support ($500-$4500/yr) Comes with UI for monitoring and history Supports Cron and ad-hoc running of jobs Allows for continuations Automatic retries Supports concurrency limiting Persists job state to database scottsauber

  33. Demo

  34. How does Hangfire work? Serializes method call and all arguments Creates background job based on that information Saves job to persistent storage Starts background job if immediate scottsauber

  35. When do I use Hangfire? Want to host jobs in ASP.NET Core Need features Hangfire offers Don t want to write plumbing code Ok with relying on a 3rd party library scottsauber

  36. When do I NOT use Hangfire? Do not want to host jobs in ASP.NET Core Have basic needs and do not need Hangfire s features Do not want to rely on 3rd party library More control over what happens scottsauber

  37. Cloud options Azure Functions Azure WebJobs AWS Lambdas GCP Cloud Scheduler + Cloud Functions Didn t cover these to avoid cloud specific scottsauber

  38. Takeaways Awareness to all the options available to you More information to make the best decision for you and your company scottsauber

  39. Resources https://docs.microsoft.com/en- us/dotnet/architecture/microservices/multi-container-microservice- net-applications/background-tasks-with-ihostedservice https://www.hangfire.io/ https://app.pluralsight.com/library/courses/building-aspnet-core- hosted-services-net-core-worker-services/ This slide deck scottsauber

  40. Questions? scottsauber Slides up at scottsauber.com

  41. Thanks! scottsauber Slides up at scottsauber.com

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#