1515using 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 }
0 commit comments