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.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Worker.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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:5001/").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 = "630ddbad89698131d98dc0fd" });
  68. Console.WriteLine(res.Item!.Name);
  69. }
  70. catch (RpcException e)
  71. {
  72. if (e.StatusCode == StatusCode.Cancelled)
  73. {
  74. return;
  75. }
  76. Console.WriteLine(e.Message);
  77. throw;
  78. }
  79. //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  80. }
  81. }
  82. }
  83. }