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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. .AddPolicyHandler(GetBulkheadPolicy(50,200));
  31. IAsyncPolicy<HttpResponseMessage> GetBulkheadPolicy(int capacity, int queueLength)
  32. {
  33. //As soon as we hit 50 concurrent requests, the policy will add subsequent requests to the pending request queue.
  34. //Once the pending request queue is full, then the policy will start rejecting any other calls to the service.
  35. return Policy.BulkheadAsync<HttpResponseMessage>(capacity, queueLength);
  36. }
  37. IAsyncPolicy<HttpResponseMessage> GetCircuitBreaker()
  38. {
  39. return HttpPolicyExtensions
  40. .HandleTransientHttpError()
  41. //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,
  42. //then the circuit should be cut for 30 seconds:
  43. .AdvancedCircuitBreakerAsync(0.25, TimeSpan.FromSeconds(60),
  44. 7, TimeSpan.FromSeconds(30), OnBreak, OnReset, OnHalfOpen);
  45. }
  46. void OnHalfOpen()
  47. {
  48. Console.WriteLine("Circuit in test mode, one request will be allowed.");
  49. }
  50. void OnReset()
  51. {
  52. Console.WriteLine("Circuit closed, requests flow normally.");
  53. }
  54. void OnBreak(DelegateResult<HttpResponseMessage> result, TimeSpan ts)
  55. {
  56. Console.WriteLine("Circuit cut, requests will not flow.");
  57. }
  58. IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
  59. {
  60. return HttpPolicyExtensions
  61. // HttpRequestException, 5XX and 408
  62. .HandleTransientHttpError()
  63. // 404
  64. .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
  65. // Retry two times after delay
  66. .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)));
  67. }
  68. builder.Services.AddControllersWithViews();
  69. builder.Services.AddRazorPages();
  70. builder.Services.AddEndpointsApiExplorer();
  71. builder.Services.AddSwaggerGen();
  72. builder.Services.AddGrpc();
  73. builder.Services.AddCodeFirstGrpc();
  74. builder.Services.AddCodeFirstGrpcReflection();
  75. var app = builder.Build();
  76. app.UseSwagger();
  77. app.UseSwaggerUI();
  78. // Configure the HTTP request pipeline.
  79. if (app.Environment.IsDevelopment())
  80. {
  81. app.UseWebAssemblyDebugging();
  82. }
  83. else
  84. {
  85. app.UseExceptionHandler("/Error");
  86. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  87. app.UseHsts();
  88. }
  89. app.UseHttpsRedirection();
  90. app.UseBlazorFrameworkFiles();
  91. app.UseStaticFiles();
  92. app.UseRouting();
  93. app.UseGrpcWeb();
  94. app.MapRazorPages();
  95. app.MapControllers();
  96. //app.MapGrpcService<AuthService>();
  97. //app.MapGrpcService<AuthService>().EnableGrpcWeb();
  98. app.MapCodeFirstGrpcReflectionService();
  99. app.MapFallbackToFile("index.html");
  100. app.Run();