Skip to content

Commit 91df5c1

Browse files
committed
Use primary constructors
1 parent 7f5bf1a commit 91df5c1

File tree

3 files changed

+29
-50
lines changed

3 files changed

+29
-50
lines changed

ProReception.DistributionServerInfrastructure/HostedServices/SignalRHostedService.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,19 @@
1515
using Settings.Models.Public;
1616

1717
[UnsupportedOSPlatform("browser")] // Proxy support in SignalR is not supported in browser
18-
public abstract class SignalRHostedService<T> : IHostedService
18+
public abstract class SignalRHostedService<T>(
19+
IOptions<ProReceptionApiConfiguration> proReceptionApiConfigurationOptions,
20+
IOptions<ProxyConfiguration> proxyConfigurationOptions,
21+
ILogger<T> logger,
22+
IProReceptionApiClient proReceptionApiClient,
23+
ISettingsManagerBase settingsManagerBase)
24+
: IHostedService
1925
{
2026
private readonly CancellationTokenSource _stoppingCts = new();
21-
private readonly ILogger<T> _logger;
22-
private readonly IProReceptionApiClient _proReceptionApiClient;
23-
private readonly ISettingsManagerBase _settingsManagerBase;
24-
private readonly ProReceptionApiConfiguration _proReceptionApiConfiguration;
25-
private readonly ProxyConfiguration _proxyConfiguration;
2627

2728
private Task? _startUpTask;
2829
private HubConnection? _hubConnection;
2930

30-
protected SignalRHostedService(
31-
IOptions<ProReceptionApiConfiguration> proReceptionApiConfigurationOptions,
32-
IOptions<ProxyConfiguration> proxyConfigurationOptions,
33-
ILogger<T> logger,
34-
IProReceptionApiClient proReceptionApiClient,
35-
ISettingsManagerBase settingsManagerBase)
36-
{
37-
_logger = logger;
38-
_proReceptionApiClient = proReceptionApiClient;
39-
_settingsManagerBase = settingsManagerBase;
40-
_proReceptionApiConfiguration = proReceptionApiConfigurationOptions.Value;
41-
_proxyConfiguration = proxyConfigurationOptions.Value;
42-
}
43-
4431
protected abstract string HubPath { get; }
4532

4633
public Task StartAsync(CancellationToken cancellationToken)
@@ -52,7 +39,7 @@ public Task StartAsync(CancellationToken cancellationToken)
5239

5340
public async Task StopAsync(CancellationToken cancellationToken)
5441
{
55-
_logger.LogInformation($"Stopping {typeof(T).Name}...");
42+
logger.LogInformation($"Stopping {typeof(T).Name}...");
5643

5744
// Stop called without start
5845
if (_startUpTask == null)
@@ -63,7 +50,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
6350
try
6451
{
6552
// Signal cancellation to the executing method
66-
_stoppingCts.Cancel();
53+
await _stoppingCts.CancelAsync();
6754
}
6855
finally
6956
{
@@ -81,7 +68,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
8168

8269
private async Task ExecuteStartUp(CancellationToken cancellationToken)
8370
{
84-
_logger.LogInformation($"Starting {typeof(T).Name}...");
71+
logger.LogInformation($"Starting {typeof(T).Name}...");
8572

8673
await new ResiliencePipelineBuilder()
8774
.AddRetry(new RetryStrategyOptions
@@ -99,7 +86,7 @@ private async Task ExecuteStartUp(CancellationToken cancellationToken)
9986
exceptionMessage += $" Response body: {await flurlException.GetResponseStringAsync()}";
10087
}
10188

102-
_logger.LogWarning("Attempt {AttemptNumber} failed: {ExceptionMessage}. Waiting {RetryDelay} before next try.", args.AttemptNumber, exceptionMessage, args.RetryDelay);
89+
logger.LogWarning("Attempt {AttemptNumber} failed: {ExceptionMessage}. Waiting {RetryDelay} before next try.", args.AttemptNumber, exceptionMessage, args.RetryDelay);
10390
}
10491
})
10592
.Build()
@@ -121,14 +108,14 @@ private async Task LoginAndCreateSignalRConnection(CancellationToken cancellatio
121108
throw new ApplicationException("The ProReception access token is null or empty (this should never happen)");
122109
}
123110

124-
_logger.LogInformation("Establishing SignalR connection...");
111+
logger.LogInformation("Establishing SignalR connection...");
125112

126113
_hubConnection = new HubConnectionBuilder()
127-
.WithUrl(_proReceptionApiConfiguration.BaseUrl.AppendPathSegment(HubPath), options =>
114+
.WithUrl(proReceptionApiConfigurationOptions.Value.BaseUrl.AppendPathSegment(HubPath), options =>
128115
{
129116
options.Headers.Add("Authorization", $"Bearer {proReceptionTokens.AccessToken}");
130-
options.Headers.Add("X-DistributionServerAppId", _settingsManagerBase.GetDistributionServerAppId().ToString());
131-
options.Proxy = _proxyConfiguration.GetWebProxy();
117+
options.Headers.Add("X-DistributionServerAppId", settingsManagerBase.GetDistributionServerAppId().ToString());
118+
options.Proxy = proxyConfigurationOptions.Value.GetWebProxy();
132119
})
133120
.WithAutomaticReconnect()
134121
.Build();
@@ -137,34 +124,34 @@ private async Task LoginAndCreateSignalRConnection(CancellationToken cancellatio
137124

138125
_hubConnection.Closed += async _ =>
139126
{
140-
_logger.LogInformation("SignalR connection lost, will retry...");
127+
logger.LogInformation("SignalR connection lost, will retry...");
141128
await _hubConnection.StopAsync(cancellationToken);
142129
await ExecuteStartUp(cancellationToken);
143130
};
144131

145132
await _hubConnection.StartAsync(cancellationToken);
146133

147-
_logger.LogInformation("SignalR connection successfully established");
134+
logger.LogInformation("SignalR connection successfully established");
148135
}
149136
}
150137

151138
private async Task<TokensRecord?> GetProReceptionTokens(CancellationToken cancellationToken)
152139
{
153140
while(!cancellationToken.IsCancellationRequested)
154141
{
155-
var proReceptionTokens = _settingsManagerBase.GetTokens();
142+
var proReceptionTokens = settingsManagerBase.GetTokens();
156143

157144
if (!string.IsNullOrWhiteSpace(proReceptionTokens?.AccessToken))
158145
{
159146
if (proReceptionTokens.ExpiresAtUtc.AddMinutes(-10) < DateTime.UtcNow)
160147
{
161-
return await _proReceptionApiClient.RefreshAndSaveTokens(proReceptionTokens);
148+
return await proReceptionApiClient.RefreshAndSaveTokens(proReceptionTokens);
162149
}
163150

164151
return proReceptionTokens;
165152
}
166153

167-
_logger.LogInformation("Not logged into ProReception, sleeping...");
154+
logger.LogInformation("Not logged into ProReception, sleeping...");
168155

169156
await Task.Delay(1000, cancellationToken);
170157
}

ProReception.DistributionServerInfrastructure/ProReceptionApi/ProReceptionApiClient.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
using ProReception.DistributionServerInfrastructure.Configuration;
77
using ProReception.DistributionServerInfrastructure.Settings;
88

9-
public class ProReceptionApiClient : ApiClientBase, IProReceptionApiClient
9+
public class ProReceptionApiClient(
10+
ILogger<ProReceptionApiClient> logger,
11+
ISettingsManagerBase settingsManagerBase,
12+
IOptions<ProReceptionApiConfiguration> options)
13+
: ApiClientBase(logger, settingsManagerBase, options), IProReceptionApiClient
1014
{
11-
public ProReceptionApiClient(ILogger<ProReceptionApiClient> logger, ISettingsManagerBase settingsManagerBase, IOptions<ProReceptionApiConfiguration> options)
12-
: base(logger, settingsManagerBase, options)
13-
{
14-
}
15-
1615
public async Task<T> Get<T>(string path) => await Query(req => req.AppendPathSegment(path).GetJsonAsync<T>());
1716

1817
public async Task<IFlurlResponse> GetRaw(string path) => await Query(req => req.AppendPathSegment(path).GetAsync());

ProReception.DistributionServerInfrastructure/Settings/LogService.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
namespace ProReception.DistributionServerInfrastructure.Settings;
22

3-
public class LogService
3+
public class LogService(ISettingsManagerBase settingsManager)
44
{
5-
private readonly ISettingsManagerBase _settingsManager;
6-
7-
public LogService(ISettingsManagerBase settingsManager)
8-
{
9-
_settingsManager = settingsManager;
10-
}
11-
125
public string GetLogFilesPath()
136
{
14-
return _settingsManager.GetLogFilesPath();
7+
return settingsManager.GetLogFilesPath();
158
}
169

1710
public IEnumerable<string> GetLogFileNames()
1811
{
19-
var directoryPath = _settingsManager.GetLogFilesPath();
12+
var directoryPath = settingsManager.GetLogFilesPath();
2013
var filePaths = Directory.EnumerateFiles(directoryPath, "*.txt");
2114

2215
return filePaths.Select(Path.GetFileName).Where(name => name != null)!;
2316
}
2417

2518
public Stream GetLogFileStream(string fileName)
2619
{
27-
var directoryPath = _settingsManager.GetLogFilesPath();
20+
var directoryPath = settingsManager.GetLogFilesPath();
2821
var filePath = Path.Combine(directoryPath, fileName);
2922

3023
if (!File.Exists(filePath))

0 commit comments

Comments
 (0)