Selaa lähdekoodia

Grpc unimplemented service bugfix

master
nemanja.grkovic 3 vuotta sitten
vanhempi
commit
f656d4e594
2 muutettua tiedostoa jossa 92 lisäystä ja 43 poistoa
  1. 2
    2
      IdentityProvider/Program.cs
  2. 90
    41
      SpotifyWorker/Worker.cs

+ 2
- 2
IdentityProvider/Program.cs Näytä tiedosto

app.UseHsts(); app.UseHsts();
} }


app.UseHttpsRedirection();
//app.UseHttpsRedirection();


//run blazor project by running grpc server //run blazor project by running grpc server
app.UseBlazorFrameworkFiles(); app.UseBlazorFrameworkFiles();
//app.MapGrpcService<AuthService>().EnableGrpcWeb(); //app.MapGrpcService<AuthService>().EnableGrpcWeb();
//app.MapGrpcService<TrackService>().EnableGrpcWeb(); //app.MapGrpcService<TrackService>().EnableGrpcWeb();
//app.MapGrpcService<StatsService>().EnableGrpcWeb(); //app.MapGrpcService<StatsService>().EnableGrpcWeb();
//app.MapGrpcService<IdentityService>().EnableGrpcWeb();
app.MapGrpcService<IdentityService>();


app.MapCodeFirstGrpcReflectionService(); app.MapCodeFirstGrpcReflectionService();



+ 90
- 41
SpotifyWorker/Worker.cs Näytä tiedosto

using Grpc.Core; using Grpc.Core;
using Grpc.Net.Client; using Grpc.Net.Client;
using GrpcShared.DTO;
using GrpcShared.Interfaces; using GrpcShared.Interfaces;
using IdentityProvider.Services;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
public class Worker : BackgroundService public class Worker : BackgroundService
{ {
private readonly ILogger<Worker> _logger; private readonly ILogger<Worker> _logger;
private IStatsService _serviceClient; private IStatsService _serviceClient;
private IIdentityService _identityService;
public Worker(ILogger<Worker> logger) public Worker(ILogger<Worker> logger)
{ {
_logger = logger; _logger = logger;
// AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
_serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService<IStatsService>(); _serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService<IStatsService>();
_identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService<IIdentityService>();
}
public override Task StartAsync(CancellationToken cancellationToken)
{
return base.StartAsync(cancellationToken);
} }
//public override Task StartAsync(CancellationToken cancellationToken)
//{
// return base.StartAsync(cancellationToken);
//}
//public class GrpcServerStartup
//{
// public void ConfigureServices(IServiceCollection services)
// {
// services.AddGrpc();
// //services.AddTransient<IStatsService, StatsService>();
// }
public class GrpcServerStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
//services.AddTransient<IStatsService, StatsService>();
}


// public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// {
// app.UseRouting();
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();


// app.UseEndpoints(endpoints =>
// {
// endpoints.MapGrpcService<StatsService>();
// });
// }
//}
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<StatsService>();
endpoints.MapGrpcService<IdentityService>();
});
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
// await Host.CreateDefaultBuilder()
//.ConfigureWebHostDefaults(builder =>
//{
// builder
// .ConfigureKestrel(options =>
// {
// options.ListenAnyIP(0, listenOptions =>
// {
// listenOptions.Protocols = HttpProtocols.Http2;
// });
// })
// .UseKestrel()
// .UseStartup<GrpcServerStartup>();
//})
//.Build()
//.StartAsync(stoppingToken);
// await Host.CreateDefaultBuilder()
//.ConfigureWebHostDefaults(builder =>
//{
// builder
// //.ConfigureKestrel(options =>
// //{
// // options.ListenAnyIP(5002, listenOptions =>
// // {
// // listenOptions.Protocols = HttpProtocols.Http2;
// // });
// //})
// .UseStartup<GrpcServerStartup>()
// .UseUrls("http://localhost:5005");
//})
//.Build()
//.StartAsync(stoppingToken);
//await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" }); //await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
//var result = await _httpClient.GetAsync("url"); //var result = await _httpClient.GetAsync("url");
//_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(5000, stoppingToken); await Task.Delay(5000, stoppingToken);
//try
//{
// var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "630ddbad89698131d98dc0fd" });
// Console.WriteLine(res.Item!.Name);

//}
//catch (RpcException e)
//{
// if (e.StatusCode == StatusCode.Cancelled)
// {
// return;
// }
// Console.WriteLine(e.Message);
// throw;
//}
try try
{ {
var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "630ddbad89698131d98dc0fd" });
Console.WriteLine(res.Item!.Name);
var res = await _identityService.ListUsersAsync(new VoidMessage());
if (res == null) return;

for (int i = 0; i < res.Count; i++)
{
try
{
var track = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage
{
UserId = res[i].Id
});
if (track != null)
{
await _identityService.SaveTrackAsync(new GrpcShared.DTO.Db.SaveTrackRequest
{
Album = track.Item.Album.Name,
Artist = track.Item.Artists.Single().Name,
Title = track.Item.Name,
TrackId = track.Item.Id,
UserId = res[i].Id
});
}
}
catch (RpcException ex)
{
if (ex.StatusCode == StatusCode.Cancelled) continue;
throw;
}

}


} }
catch (RpcException e) catch (RpcException e)
{ {
return; return;
} }
Console.WriteLine(e.Message);
//Console.WriteLine(e.Message);
throw; throw;
} }


//res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" }); //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });





Loading…
Peruuta
Tallenna