BigscreenDataShowRefreshService.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Hotline.Realtimes;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Hosting;
  9. using Microsoft.Extensions.Logging;
  10. namespace Hotline.Application.Bigscreen
  11. {
  12. public class BigscreenDataShowRefreshService : BackgroundService
  13. {
  14. private readonly IServiceScopeFactory _serviceScopeFactory;
  15. public BigscreenDataShowRefreshService(IServiceScopeFactory serviceScopeFactory)
  16. {
  17. _serviceScopeFactory = serviceScopeFactory;
  18. }
  19. /// <summary>
  20. /// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The implementation should return a task that represents
  21. /// the lifetime of the long running operation(s) being performed.
  22. /// </summary>
  23. /// <param name="stoppingToken">Triggered when <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> is called.</param>
  24. /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running operations.</returns>
  25. /// <remarks>See <see href="https://docs.microsoft.com/dotnet/core/extensions/workers">Worker Services in .NET</see> for implementation guidelines.</remarks>
  26. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  27. {
  28. using var scope = _serviceScopeFactory.CreateScope();
  29. var realtimeService = scope.ServiceProvider.GetRequiredService<IRealtimeService>();
  30. while (!stoppingToken.IsCancellationRequested)
  31. {
  32. //todo
  33. var data = new {
  34. Name = "John",
  35. Age = 20,
  36. };
  37. await realtimeService.BsDataShowChangedAsync(data, stoppingToken);
  38. await Task.Delay(30000, stoppingToken);
  39. }
  40. }
  41. }
  42. }