asp.net-core-hosted-services

“timer + Task.Run” vs “while loop + Task.Delay” in asp.net core hosted service

不羁岁月 提交于 2021-02-20 04:30:07
问题 I have a requirement that background service should run Process method every day at 0:00 a.m. So, one of my team member wrote the following code: public class MyBackgroundService : IHostedService, IDisposable { private readonly ILogger _logger; private Timer _timer; public MyBackgroundService(ILogger<MyBackgroundService> logger) { _logger = logger; } public void Dispose() { _timer?.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { TimeSpan interval = TimeSpan

How to pass data from a controller to a background service

淺唱寂寞╮ 提交于 2021-02-08 10:20:57
问题 I'm trying to write a simple web api project with a background service executing a method every second. This method loops over a list and just prints out (for the moment) some statistics. I declared a new list in the constructor of the background service. I'm currently encountering some issues with adding and removing items from inside the controller. public UsersController(IUserBackgroundService userService) { private readonly IUserBackgroundService _userService; public UserController

How to ensure only one instance of IHostedService is running within a .NET core 2.2 Web API

北城以北 提交于 2021-01-28 21:02:36
问题 I have a .NET Core 2.2 web API that can be scaled out to several instances based on demand. This API has a background service (IHostedService) that should only run on one of the instances at a time. Is there a common approach or design pattern to ensure this service is only running on one instance at any given point in time? 回答1: The ability to include a hosted service along with the actual application is just like anything else in Core. You can use all of MVC, API and Razor Pages, all in the

How to ensure only one instance of IHostedService is running within a .NET core 2.2 Web API

怎甘沉沦 提交于 2021-01-28 20:40:29
问题 I have a .NET Core 2.2 web API that can be scaled out to several instances based on demand. This API has a background service (IHostedService) that should only run on one of the instances at a time. Is there a common approach or design pattern to ensure this service is only running on one instance at any given point in time? 回答1: The ability to include a hosted service along with the actual application is just like anything else in Core. You can use all of MVC, API and Razor Pages, all in the

How to run multiple task in background service in .net core with different timer duration

巧了我就是萌 提交于 2020-08-05 07:11:16
问题 I am creating a worker service which will be run as a windows service. I have a requirement where I would like to invoke two tasks which may have different timers. Say DoWork should be called every 5 minutes and DoAnotherWork should be called every 10 minutes or so. These two tasks can run in parallel and are not dependant on each other. I was able to create task DoWork which can run after every 5 minutes. I am a bit confused about how to implement another task that will have different timer

How can i write unit test for my background service?

て烟熏妆下的殇ゞ 提交于 2020-03-18 06:31:05
问题 I'm working with the HostBuilder in .NET Core (not the WebHost !). I have one Hosted Service running in my application that override the ExecuteAsync/StopAsync Method of the background Service and I want to unit test it. Here is my HostedService: public class DeviceToCloudMessageHostedService : BackgroundService { private readonly IDeviceToCloudMessageService _deviceToCloudMessageService; private readonly AppConfig _appConfig; public DeviceToCloudMessageHostedService

Task.Delay() triggers cancellation of application lifetime

感情迁移 提交于 2019-12-24 19:16:40
问题 I have faced a situation when Task.Delay() method would trigger an event of cancellation in IApplicationLifetime . Here is the code: static async Task Main(string[] args) { Console.WriteLine("Starting"); await BuildWebHost(args) .RunAsync(); Console.WriteLine("Press any key to exit.."); Console.ReadKey(); } private static IHost BuildWebHost(string[] args) { var hostBuilder = new HostBuilder() .ConfigureHostConfiguration(config => { config.AddEnvironmentVariables(); config.AddCommandLine(args)