Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Worker.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Grpc.Core;
  2. using Grpc.Net.Client;
  3. using GrpcShared.Interfaces;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Server.Kestrel.Core;
  7. using ProtoBuf.Grpc.Client;
  8. using SpotifyService.Services;
  9. namespace SpotifyWorker
  10. {
  11. public class Worker : BackgroundService
  12. {
  13. private readonly ILogger<Worker> _logger;
  14. private IStatsService _serviceClient;
  15. public Worker(ILogger<Worker> logger)
  16. {
  17. _logger = logger;
  18. // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
  19. _serviceClient = GrpcChannel.ForAddress("https://localhost:5051/").CreateGrpcService<IStatsService>();
  20. }
  21. //public override Task StartAsync(CancellationToken cancellationToken)
  22. //{
  23. // return base.StartAsync(cancellationToken);
  24. //}
  25. //public class GrpcServerStartup
  26. //{
  27. // public void ConfigureServices(IServiceCollection services)
  28. // {
  29. // services.AddGrpc();
  30. // //services.AddTransient<IStatsService, StatsService>();
  31. // }
  32. // public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  33. // {
  34. // app.UseRouting();
  35. // app.UseEndpoints(endpoints =>
  36. // {
  37. // endpoints.MapGrpcService<StatsService>();
  38. // });
  39. // }
  40. //}
  41. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  42. {
  43. while (!stoppingToken.IsCancellationRequested)
  44. {
  45. // await Host.CreateDefaultBuilder()
  46. //.ConfigureWebHostDefaults(builder =>
  47. //{
  48. // builder
  49. // .ConfigureKestrel(options =>
  50. // {
  51. // options.ListenAnyIP(0, listenOptions =>
  52. // {
  53. // listenOptions.Protocols = HttpProtocols.Http2;
  54. // });
  55. // })
  56. // .UseKestrel()
  57. // .UseStartup<GrpcServerStartup>();
  58. //})
  59. //.Build()
  60. //.StartAsync(stoppingToken);
  61. //await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  62. //var result = await _httpClient.GetAsync("url");
  63. //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  64. await Task.Delay(5000, stoppingToken);
  65. try
  66. {
  67. var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  68. }
  69. catch (RpcException e)
  70. {
  71. if (e.StatusCode == StatusCode.Cancelled)
  72. {
  73. return;
  74. }
  75. Console.WriteLine(e.Message);
  76. throw;
  77. }
  78. //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  79. }
  80. }
  81. }
  82. }