.NET Hosted Service: Run a background service and task (uses .NET Core)

Published: 30 January 2022
on channel: Round The Code
19,418
263

You can use a Hosted Service from .NET Core 3.1 and they are an excellent way of running a C# background task.

These background services can be run in an ASP.NET Core web application, which is ideal if you need update data that would affect all users.

We look at how to create a hosted service, and what the difference is between inheriting the IHostedService interface and the BackgroundService abstract class.

In addition, we'll find out the difference between starting a background task using the StartAsync method against the ExecuteAsync method.

Afterwards, we'll demonstrate how to add hosted services to the IServiceCollection.

Finally, we'll see how hosted services reacted with dependency injection, and why hosted services can successfully resolve a singleton and transient service, but not a scoped service.

💻 Download the code example for this demo 💻
https://www.roundthecode.com/dotnet-c...

📖 Learn .NET and C# with our online courses 📖
https://www.roundthecode.com/dotnet-c...

► More information: https://www.roundthecode.com/dotnet-t...

► Chapters
0:00 The hosted services area covered
0:48 Creating a hosted service
2:51 Add a hosted service to IServiceCollection
5:10 How dependency injection works
8:37 Should we use a worker service?

#hostedservice #aspnetcore #dependencyinjection


Watch video .NET Hosted Service: Run a background service and task (uses .NET Core) online without registration, duration hours minute second in high quality. This video was added by user Round The Code 30 January 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 19,418 once and liked it 263 people.