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.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Program.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.AspNetCore.Server.Kestrel.Core;
  2. using NemAnBlazor.Services.Interfaces;
  3. using NemAnBlazor.Services;
  4. using ProtoBuf.Grpc.Server;
  5. using SpotifyService.Services;
  6. using GrpcShared.Interfaces;
  7. using Polly;
  8. using Polly.Extensions.Http;
  9. var builder = WebApplication.CreateBuilder(args);
  10. #if DEBUG
  11. /*
  12. builder.WebHost.ConfigureKestrel(options =>
  13. {
  14. options.ListenLocalhost(5050, o => o.Protocols =
  15. HttpProtocols.Http2);
  16. options.ListenLocalhost(5051, o => o.Protocols =
  17. HttpProtocols.Http1AndHttp2);
  18. });
  19. */
  20. #endif
  21. // Add services to the container.
  22. builder.Services.AddHttpClient("HttpClient", c =>
  23. {
  24. c.BaseAddress = new Uri(SpotifyService.GLOBALS.SPOTIFYURL);
  25. c.DefaultRequestHeaders.Add("Accept", SpotifyService.GLOBALS.MEDIATYPE);
  26. })
  27. .SetHandlerLifetime(TimeSpan.FromMinutes(5))
  28. .AddPolicyHandler(GetRetryPolicy())
  29. .AddPolicyHandler(GetCircuitBreaker());
  30. IAsyncPolicy<HttpResponseMessage> GetCircuitBreaker()
  31. {
  32. return HttpPolicyExtensions
  33. .HandleTransientHttpError()
  34. //the circuit will be cut if 25% of requests fail in a 60 second window, with a minimum of 7 requests in the 60 second window,
  35. //then the circuit should be cut for 30 seconds:
  36. .AdvancedCircuitBreakerAsync(0.25, TimeSpan.FromSeconds(60),
  37. 7, TimeSpan.FromSeconds(30), OnBreak, OnReset, OnHalfOpen);
  38. }
  39. void OnHalfOpen()
  40. {
  41. Console.WriteLine("Circuit in test mode, one request will be allowed.");
  42. }
  43. void OnReset()
  44. {
  45. Console.WriteLine("Circuit closed, requests flow normally.");
  46. }
  47. void OnBreak(DelegateResult<HttpResponseMessage> result, TimeSpan ts)
  48. {
  49. Console.WriteLine("Circuit cut, requests will not flow.");
  50. }
  51. IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
  52. {
  53. return HttpPolicyExtensions
  54. // HttpRequestException, 5XX and 408
  55. .HandleTransientHttpError()
  56. // 404
  57. .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
  58. // Retry two times after delay
  59. .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)));
  60. }
  61. builder.Services.AddControllersWithViews();
  62. builder.Services.AddRazorPages();
  63. builder.Services.AddEndpointsApiExplorer();
  64. builder.Services.AddSwaggerGen();
  65. builder.Services.AddGrpc();
  66. builder.Services.AddCodeFirstGrpc();
  67. builder.Services.AddCodeFirstGrpcReflection();
  68. var app = builder.Build();
  69. app.UseSwagger();
  70. app.UseSwaggerUI();
  71. // Configure the HTTP request pipeline.
  72. if (app.Environment.IsDevelopment())
  73. {
  74. app.UseWebAssemblyDebugging();
  75. }
  76. else
  77. {
  78. app.UseExceptionHandler("/Error");
  79. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  80. app.UseHsts();
  81. }
  82. app.UseHttpsRedirection();
  83. app.UseBlazorFrameworkFiles();
  84. app.UseStaticFiles();
  85. app.UseRouting();
  86. app.UseGrpcWeb();
  87. app.MapRazorPages();
  88. app.MapControllers();
  89. //app.MapGrpcService<AuthService>();
  90. //app.MapGrpcService<AuthService>().EnableGrpcWeb();
  91. app.MapCodeFirstGrpcReflectionService();
  92. app.MapFallbackToFile("index.html");
  93. app.Run();