Avoid IHostedService before .NET 8 if you can, because it has a problem... 👇 In .NET versions before 8, the 𝐒𝐭𝐚𝐫𝐭𝐀𝐬𝐲𝐧𝐜 methods of 𝐈𝐇𝐨𝐬𝐭𝐞𝐝𝐒𝐞𝐫𝐯𝐢𝐜𝐞 implementations were called one after the other during application startup. Each StartAsync method had to be finished before the next one could begin, which could delay the startup if any service took a long time to initialize. Imagine you have a task that executes for a couple of minutes. This means the application would not start until that task is finished. .NET 8 introduces a solution with the 𝐒𝐞𝐫𝐯𝐢𝐜𝐞𝐬𝐒𝐭𝐚𝐫𝐭𝐂𝐨𝐧𝐜𝐮𝐫𝐫𝐞𝐧𝐭𝐥𝐲 property in the HostOptions class. With services now starting concurrently, the host’s startup time is minimized to the duration of the slowest service's StartAsync method, rather than being the combined time of all services. Note: Each service begins only after the previous one releases control by reaching its first await statement in the StartAsync method. To allow the next service to start as quickly as possible, ensure that StartAsync is truly asynchronous and avoid any costly operations before the first await. Like the post? You'll enjoy with 13k+ engineers in this free community TheCodeMan.net.
Good insights. However, sometimes or even quit often I needed to run these services one by one and make sure my Host is started after all these services finished their work. For example: initing in-memory cache, executing migrations etc. If I ever need to run a long-running I would rather do it in Background Service, not to slow down my host startup. Another place to create some initialize logic, is in Program.cs before app.Run(). You can eat your cache, run migrations etc
Yippee! Exciting to see the improvements in .NET 8 for faster application startups. Thanks for the heads up on avoiding IHostedService in earlier versions! Stefan Đokić
Nice catch, however if you follow Microsoft recommendations which explicitly tell that you shouldn’t use IHostedService for long running operations then there would be no issues. Useful option to run in parallel!
https://www.siteox.com/announcements/45/.NET-Development-on-Linux-x86_64-and-ppc64le.html Podman Containers and .NET (dotnet) on Linux ppc64le and x86_64 in the Cloud at SiteOx.com/linux - Daily Weekly Monthly leasing - Cost Effective - On-Demand - Automated Deployment - Ready in Minutes #siteox #automation #automated #deployment #cloud #datacenter #migration #aix #hpux #solaris #linux #oraclelinux #powerpc #powervm #powerha #powervc #ppc64 #ppc64le #hpia64 #sparc #DotNet #DotNetCore #DotNet8 #DotNet9 #CSharp #ASPNet #ASPNetCore #VisualStudio #Blazor #EntityFramework #NuGet #MVC #WebAPI #CodeFirst #MediatR #UnitTesting #DevOps #startups #dotnet #Startup #Tech #DotNetDevelopment #TechForStartups #CrossPlatformDevelopment #StartupGrowth #ScalableSolutions #DotNetCore #AzureDevelopment #TechHiring #SoftwareDevelopment #CloudIntegration #StartupSuccess #FullStackDevelopers #MicroservicesArchitecture #iqlance #Dotne tsolutions #Business #Guide
One of great improvements vs. previous versions. This sequential execution many times caused me a serious headache 😅
Stefan Đokić Do you have idea to deal with multiple instances of API running at the same time? For example, we deploy the API to K8S and scale out Pods. Does the hosted service process multiple time?
Did you try this in production?
Useful tips
➡️ I help you to improve your .NET knowledge! | Microsoft MVP
1mo📌 Bonus: Background Tasks in .NET 8: https://thecodeman.net/posts/background-tasks-in-dotnet8?utm_source=LinkedIn