Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HostedReturnBookService : IHostedService
- {
- private readonly IServiceProvider serviceProvider;
- private Timer timer;
- public HostedReturnBookService(IServiceProvider serviceProvider)
- {
- this.serviceProvider = serviceProvider;
- }
- public Task StartAsync(CancellationToken cancellationToken)
- {
- this.timer = new Timer(ReturnBook, null, TimeSpan.FromDays(1),
- TimeSpan.FromDays(1));
- return Task.CompletedTask;
- }
- private async void ReturnBook(object state)
- {
- using (var scope = this.serviceProvider.CreateScope())
- {
- var checkoutService = scope.ServiceProvider.GetRequiredService<ICheckoutService>();
- await checkoutService.CheckForExpiredCheckoutsAsync();
- }
- }
- public Task StopAsync(CancellationToken cancellationToken)
- {
- this.timer?.Change(Timeout.Infinite, 0);
- return Task.CompletedTask;
- }
- }
- //GOES IN STARTUP DI REGISTRATION
- services.AddHostedService<HostedReturnBookService>();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement