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



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

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

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 sample for this demo 💻
https://www.roundthecode.com/dotnet-samples/asp-net-core-hosted-service-dotnet-6?utm_source=youtube&utm_medium=referral&utm_campaign=IekoUio2Fek+-+video+content+%28code+sample%29

📖 Learn .NET and C# with our online courses 📖
https://www.roundthecode.com/dotnet-c-sharp-courses?utm_source=youtube&utm_medium=referral&utm_campaign=IekoUio2Fek+-+video+content+%28online+course%29

â–º More information: https://www.roundthecode.com/dotnet/use-asp-net-core-hosted-services-run-background-task?utm_source=youtube&utm_medium=referral&utm_campaign=IekoUio2Fek+-+video+content+%28more+information%29

â–º 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

Comments are closed.