Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1.     public class HostedReturnBookService : IHostedService
  2.     {
  3.         private readonly IServiceProvider serviceProvider;
  4.         private Timer timer;
  5.  
  6.         public HostedReturnBookService(IServiceProvider serviceProvider)
  7.         {
  8.             this.serviceProvider = serviceProvider;
  9.         }
  10.  
  11.         public Task StartAsync(CancellationToken cancellationToken)
  12.         {
  13.             this.timer = new Timer(ReturnBook, null, TimeSpan.FromDays(1),
  14.                 TimeSpan.FromDays(1));
  15.  
  16.             return Task.CompletedTask;
  17.         }
  18.  
  19.         private async void ReturnBook(object state)
  20.         {
  21.             using (var scope = this.serviceProvider.CreateScope())
  22.             {
  23.                 var checkoutService = scope.ServiceProvider.GetRequiredService<ICheckoutService>();
  24.  
  25.                 await checkoutService.CheckForExpiredCheckoutsAsync();
  26.             }
  27.         }
  28.  
  29.         public Task StopAsync(CancellationToken cancellationToken)
  30.         {
  31.             this.timer?.Change(Timeout.Infinite, 0);
  32.  
  33.             return Task.CompletedTask;
  34.         }
  35.     }
  36.  
  37.  
  38. //GOES IN STARTUP DI REGISTRATION
  39.             services.AddHostedService<HostedReturnBookService>();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement