[{"data":1,"prerenderedAt":92},["ShallowReactive",2],{"/2026/01/22-multiple-backgroundservice-or-ihostedservices-but-only-one-works":3},{"id":4,"title":5,"body":6,"date":83,"description":44,"extension":84,"meta":85,"navigation":86,"path":87,"robots":88,"seo":89,"stem":90,"__hash__":91},"posts/2026/01/22 multiple-BackgroundService-or-IHostedServices-but-only-one-works.md","22 Multiple BackgroundService Or IHostedServices But Only One Works",{"type":7,"value":8,"toc":80},"minimark",[9,18,25,28,31,45,47,50,52,67,69,72],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"multiple-backgroundservice-or-ihostedservices-but-only-one-works","Multiple BackgroundService or IHostedServices but Only One Works",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 9/2/2024.",[26,27],"br",{},[22,29,30],{},"In my worker app, I attempted to add multiple hosted services as follow:",[32,33,34],"code-block",{},[35,36,41],"pre",{"className":37,"code":39,"language":40},[38],"language-text","builder.Services\n    .addSingleton(HostedService1)\n    .addSingleton(HostedService2)\n    .addSingleton(HostedService3);\n","text",[42,43,39],"code",{"__ignoreMap":44},"",[26,46],{},[22,48,49],{},"All the hosted services are added, but when the application run, only 1 is executing.",[26,51],{},[22,53,54,55,66],{},"Thanks to Stephen Cleary, apparently issue with synchronous call. ",[56,57,60],"span",{"className":58},[59],"text-blue-600",[61,62,63],"a",{"href":63,"rel":64},"https://blog.stephencleary.com/2020/05/backgroundservice-gotcha-startup.html",[65],"nofollow",".",[26,68],{},[22,70,71],{},"I ended up using Task.Run for code that executes for a long time. Inside the ExecuteAsync:",[32,73,74],{},[35,75,78],{"className":76,"code":77,"language":40},[38],"await Task.Run(async () => await LongRunningProcess());\n",[42,79,77],{"__ignoreMap":44},{"title":44,"searchDepth":81,"depth":81,"links":82},2,[],"2026-01-22T00:00:00.000Z","md",{},true,"/2026/01/22-multiple-backgroundservice-or-ihostedservices-but-only-one-works",null,{"title":5,"description":44},"2026/01/22 multiple-BackgroundService-or-IHostedServices-but-only-one-works","ac0v3t4JkKLpram6AISOFyu2nVVkqqFl_PawsznAH5M",1785167451717]