Answered by:
How to use StartAsync and StopAsync of IHostedService in controller?

Question
-
User-467044538 posted
Hi, I am new to ASP.NET Core.
I've learnt that the hosted service is started when the server is started.
I have question on how to use StartAsync and StopAsync method of IHostedService from controller?
Thanks.
Monday, June 10, 2019 8:41 AM
Answers
-
User-854763662 posted
Hi Frank420 ,
You could follow below step by step:
1.Create IHostedService
public class RecureHostedService : IHostedService, IDisposable { private readonly ILogger _log; private Timer_timer; public RecureHostedService (ILogger <RecureHostedService> log) { _log = log; } public void Dispose() { _timer.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { _log.LogInformation("RecureHostedService is Starting"); _timer = new Timer(DoWork,null,TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _log.LogInformation("RecureHostedService is Stopping"); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } private void DoWork(object state) { _log.LogInformation("Timed Background Service is working."); } }
2. Register IHostedService
services.AddSingleton<IHostedService, RecureHostedService>();
3.Start and Stop Service
public class HomeController : Controller { private readonly RecureHostedService _recureHostedService; public HomeController(IHostedService hostedService) { _recureHostedService = hostedService as RecureHostedService; } public IActionResult About() { ViewData["Message"] = "Your application description page."; _recureHostedService.StopAsync(new System.Threading.CancellationToken()); return View(); } public IActionResult Contact() { ViewData["Message"] = "Your contact page."; _recureHostedService.StartAsync(new System.Threading.CancellationToken()); return View(); } }
Reference : Background tasks with hosted services in ASP.NET Core
Best Regards ,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 11, 2019 3:11 AM
All replies
-
User475983607 posted
Hi, I am new to ASP.NET Core.
I've learnt that the hosted service is started when the server is started.
I have question on how to use StartAsync and StopAsync method of IHostedService from controller?
Thanks.
As far as I know, controllers do not start and stop hosted services. This is by design because generally you do not want a controller to manage hosted services.
If you need a controller to manage a process then you can write a background service.
Monday, June 10, 2019 11:16 AM -
User-467044538 posted
Hi mgebhard, thanks for the reply.
Any good source to start with the background service?
Thanks for the information.
Monday, June 10, 2019 12:23 PM -
User-1038772411 posted
Hello Frank420,
Please refer the following links, it is giving you in detail how to use StartAsync and StopAsync method of IHostedService from controller
2 ) https://www.stevejgordon.co.uk/asp-net-core-2-ihostedservice
I hope that will help you
Thank You.
Monday, June 10, 2019 12:36 PM -
User475983607 posted
Any good source to start with the background service?Write code in await Task.Run() or the BackgroundWorker.
Monday, June 10, 2019 12:51 PM -
User-854763662 posted
Hi Frank420 ,
You could follow below step by step:
1.Create IHostedService
public class RecureHostedService : IHostedService, IDisposable { private readonly ILogger _log; private Timer_timer; public RecureHostedService (ILogger <RecureHostedService> log) { _log = log; } public void Dispose() { _timer.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { _log.LogInformation("RecureHostedService is Starting"); _timer = new Timer(DoWork,null,TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _log.LogInformation("RecureHostedService is Stopping"); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } private void DoWork(object state) { _log.LogInformation("Timed Background Service is working."); } }
2. Register IHostedService
services.AddSingleton<IHostedService, RecureHostedService>();
3.Start and Stop Service
public class HomeController : Controller { private readonly RecureHostedService _recureHostedService; public HomeController(IHostedService hostedService) { _recureHostedService = hostedService as RecureHostedService; } public IActionResult About() { ViewData["Message"] = "Your application description page."; _recureHostedService.StopAsync(new System.Threading.CancellationToken()); return View(); } public IActionResult Contact() { ViewData["Message"] = "Your contact page."; _recureHostedService.StartAsync(new System.Threading.CancellationToken()); return View(); } }
Reference : Background tasks with hosted services in ASP.NET Core
Best Regards ,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 11, 2019 3:11 AM -
User-467044538 posted
Thanks sir. Yours is clean and straightforward.
Wednesday, June 12, 2019 9:26 AM