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.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Worker.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Grpc.Core;
  2. using Grpc.Net.Client;
  3. using GrpcShared.DTO;
  4. using GrpcShared.Interfaces;
  5. using IdentityProvider.Services;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Server.Kestrel.Core;
  9. using ProtoBuf.Grpc.Client;
  10. using SpotifyService.Services;
  11. namespace SpotifyWorker
  12. {
  13. public class Worker : BackgroundService
  14. {
  15. private readonly ILogger<Worker> _logger;
  16. private IStatsService _serviceClient;
  17. private IIdentityService _identityService;
  18. public Worker(ILogger<Worker> logger)
  19. {
  20. _logger = logger;
  21. // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
  22. _serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService<IStatsService>();
  23. _identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService<IIdentityService>();
  24. }
  25. public override Task StartAsync(CancellationToken cancellationToken)
  26. {
  27. return base.StartAsync(cancellationToken);
  28. }
  29. public class GrpcServerStartup
  30. {
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. services.AddGrpc();
  34. //services.AddTransient<IStatsService, StatsService>();
  35. }
  36. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  37. {
  38. app.UseRouting();
  39. app.UseEndpoints(endpoints =>
  40. {
  41. endpoints.MapGrpcService<StatsService>();
  42. endpoints.MapGrpcService<IdentityService>();
  43. });
  44. }
  45. }
  46. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  47. {
  48. while (!stoppingToken.IsCancellationRequested)
  49. {
  50. // await Host.CreateDefaultBuilder()
  51. //.ConfigureWebHostDefaults(builder =>
  52. //{
  53. // builder
  54. // //.ConfigureKestrel(options =>
  55. // //{
  56. // // options.ListenAnyIP(5002, listenOptions =>
  57. // // {
  58. // // listenOptions.Protocols = HttpProtocols.Http2;
  59. // // });
  60. // //})
  61. // .UseStartup<GrpcServerStartup>()
  62. // .UseUrls("http://localhost:5005");
  63. //})
  64. //.Build()
  65. //.StartAsync(stoppingToken);
  66. //await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  67. //var result = await _httpClient.GetAsync("url");
  68. //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  69. await Task.Delay(5000, stoppingToken);
  70. //try
  71. //{
  72. // var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "630ddbad89698131d98dc0fd" });
  73. // Console.WriteLine(res.Item!.Name);
  74. //}
  75. //catch (RpcException e)
  76. //{
  77. // if (e.StatusCode == StatusCode.Cancelled)
  78. // {
  79. // return;
  80. // }
  81. // Console.WriteLine(e.Message);
  82. // throw;
  83. //}
  84. try
  85. {
  86. var res = await _identityService.ListUsersAsync(new VoidMessage());
  87. if (res == null) return;
  88. for (int i = 0; i < res.Count; i++)
  89. {
  90. try
  91. {
  92. var track = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage
  93. {
  94. UserId = res[i].Id
  95. });
  96. Console.WriteLine(track.IsSaved);
  97. if (track != null)
  98. {
  99. await _identityService.SaveTrackAsync(new GrpcShared.DTO.Db.SaveTrackRequest
  100. {
  101. Album = track.Item.Album.Name,
  102. Artist = track.Item.Artists[0].Name,
  103. Title = track.Item.Name,
  104. TrackId = track.Item.Id,
  105. UserId = res[i].Id
  106. });
  107. }
  108. }
  109. catch (RpcException ex)
  110. {
  111. if (ex.StatusCode == StatusCode.Cancelled) continue;
  112. throw;
  113. }
  114. }
  115. }
  116. catch (RpcException e)
  117. {
  118. if (e.StatusCode == StatusCode.Cancelled)
  119. {
  120. return;
  121. }
  122. //Console.WriteLine(e.Message);
  123. throw;
  124. }
  125. //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  126. }
  127. }
  128. }
  129. }