Background Tasks Are Finally Fixed in .NET 8

preview_player
Показать описание


Hello, everybody, I'm Nick, and in this video, I will show you how Background Tasks, Background Services or Hosted Services have changed in .NET 8. The changed in .NET 8 ultimately fix a lot of the complaints we had in previous versions.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

Classic 'hellœverybody' always brightens my day

serus
Автор

OOOHHhh man if this new feature was released a few weeks earlier it would have saved me a lot of time. I had the exact same problem with worker-services management and had to come up with a custom solution. After this video I will update my project to .Net 8 :)
This video seems to be made especially for me, thanks for everything. You are the best!

veselinvalkanov
Автор

I implemented my own version of IHostedLifecycleService with exactly the same functionality for my pet project. I had a bunch of background processes sitting side by side and posting stuff to the shared in-memory message bus. Discord bot, Twitch chat bot, Twitch API polling, database storage abstraction, and REST API to manage all that. All have to start concurrently, and all have to be aware of the whole lifecycle of the application (starting, started, stopping, stopped), so they don't message into the void and clean stuff up properly. And I build the first PoC for .NET Core 2 which is now as old as a first-grader, haha

Nekroido
Автор

Thank you Nick. Great explanation! I think BeforeStart, Start and AfterStart would be less confusing.

Kingside
Автор

i did use background service for many things and usually with the new timer which i found out about on this channel, thanks

vmachacek
Автор

I think we should also have a per-service boolean that indicates whether the service can start in parallel or not. You might have multiple background/hosted services, and some of them might need to start before the application starts, while others might not care as much

deeplerg
Автор

Adding 'await Task.Yield' as the first statement will also prevent the background service from blocking

mortentoudahl
Автор

If you look at the implementation of the BackgroundService class you will see how starting with Yield or Delay will make it non-blocking for other services starting. I am always amazed that most programmers don't get that the first part of the async method executed in the time slice of the caller. Until it yields. Using Task to start off async work is thus tricky and that is where the blocking issue originates.

jelle
Автор

Still missing feature is what to do if I wanted some of the services to start concurrently and some in strict order?

serus
Автор

What would be the best way to execute a background service on a schedule now? I think I remember you have a video about this in the past, but what is best now with .net 8 and these improvements? Say, like, I want to execute a job every hour, or at 01:00 AM every day.

JohanNordberg
Автор

Simple reason to use a 'blocking' hosted service: DB migrations. Your migration might take a bit of time. Don't want the web app to start accepting connections when the migration hasn't completed - could mean your code would error because the DB is not in the correct state.

Background hosted service not blocking are also great for other things, instead of having to use hangfire or Azure Functions etc. (keep them light weight though, peeps. might need to consider scaling out, etc).

JustinAdler
Автор

I have question here about this background service, once we deploy the web app to IIS the background service would not work until a request coming from any client!, otherwise it will not started
how could i fix this ?

OsamaALSHABANi
Автор

We use background/hosted services a lot. Typically we solve the blocking nature of them by starting worker task in startasync, storing task in private member and cancelling it in stopasync.

kjashzhvyktsul
Автор

Excellent content! I searched here but I couldn't find the explanatory video about the differences between Background Services and HostedService, could you give me the link please?

cleitoncarlospereira
Автор

Never had this issue. But i usually startup immediately and cache stuff later vs heavy startup.

T___Brown
Автор

Thanks Nick. Any thoughts on unit testing the BackgroundService?

charlesthompson
Автор

Let me see if I understand correctly: the new interface IHostedLifecycleService should be used when you need to execute something once at application start (or stop), and the BackgroundService should still be used for tasks like periodically polling a queue?

bjmmedeiros
Автор

We use background services for db migrstions and to keep observables for signal R or other reactive tasks.

garcipat
Автор

I think the only way to rationalise the names is that they used start and stop already and needed a name for an event before start and stop, and starting and stopping was the best they could come up with

namewastaken
Автор

Excelent stuff coming up in .NET 8, thanks for the update Nick! Great video!

rogeriobarretto