-
Notifications
You must be signed in to change notification settings - Fork 4
/
ServiceManager.cs
40 lines (32 loc) · 1.26 KB
/
ServiceManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Grpc.Net.Client;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using static Webradio.Service.Webradio;
namespace Webradio.Service;
public class ServiceManager : IServiceManager
{
private readonly Dictionary<string, WebradioService> services = new Dictionary<string, WebradioService>();
private readonly ILogger<ServiceManager> logger;
public ServiceManager(ILogger<ServiceManager> logger)
{
this.logger = logger;
RegisterService("soundcloud", "http://webradio-soundcloud-service/");
RegisterService("youtube", "http://webradio-youtube-service/");
}
private void RegisterService(string serviceName, string address)
{
var options = new GrpcChannelOptions()
{
ThrowOperationCanceledOnCancellation = true,
};
GrpcChannel channel = GrpcChannel.ForAddress(address, options);
var client = new WebradioClient(channel);
services.Add(serviceName, new WebradioService(client));
logger.LogInformation("Service {Name} (address: {Address}) has been registered", serviceName, address);
}
public WebradioService GetService(string serviceName)
{
services.TryGetValue(serviceName, out var client);
return client;
}
}