浏览代码

Grpc unimplemented service bugfix

master
nemanja.grkovic 3 年前
父节点
当前提交
f656d4e594
共有 2 个文件被更改,包括 92 次插入43 次删除
  1. 2
    2
      IdentityProvider/Program.cs
  2. 90
    41
      SpotifyWorker/Worker.cs

+ 2
- 2
IdentityProvider/Program.cs 查看文件

@@ -63,7 +63,7 @@ else
app.UseHsts();
}

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

//run blazor project by running grpc server
app.UseBlazorFrameworkFiles();
@@ -81,7 +81,7 @@ app.MapControllers();
//app.MapGrpcService<AuthService>().EnableGrpcWeb();
//app.MapGrpcService<TrackService>().EnableGrpcWeb();
//app.MapGrpcService<StatsService>().EnableGrpcWeb();
//app.MapGrpcService<IdentityService>().EnableGrpcWeb();
app.MapGrpcService<IdentityService>();

app.MapCodeFirstGrpcReflectionService();


+ 90
- 41
SpotifyWorker/Worker.cs 查看文件

@@ -1,6 +1,8 @@
using Grpc.Core;
using Grpc.Net.Client;
using GrpcShared.DTO;
using GrpcShared.Interfaces;
using IdentityProvider.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
@@ -12,64 +14,110 @@ namespace SpotifyWorker
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private IStatsService _serviceClient;
private IIdentityService _identityService;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
// AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
_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)
{
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" });
//var result = await _httpClient.GetAsync("url");
//_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
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
{
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)
@@ -78,10 +126,11 @@ namespace SpotifyWorker
{
return;
}
Console.WriteLine(e.Message);
//Console.WriteLine(e.Message);
throw;
}


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



正在加载...
取消
保存