@@ -18,7 +18,6 @@ namespace Microsoft.AspNetCore.Components.Server.Circuits
18
18
{
19
19
internal class CircuitHost : IAsyncDisposable
20
20
{
21
- private readonly SemaphoreSlim HandlerLock = new SemaphoreSlim ( 1 ) ;
22
21
private readonly IServiceScope _scope ;
23
22
private readonly CircuitOptions _options ;
24
23
private readonly CircuitHandler [ ] _circuitHandlers ;
@@ -187,143 +186,113 @@ private async Task OnCircuitOpenedAsync(CancellationToken cancellationToken)
187
186
{
188
187
Log . CircuitOpened ( _logger , Circuit . Id ) ;
189
188
190
- await HandlerLock . WaitAsync ( cancellationToken ) ;
189
+ Renderer . Dispatcher . AssertAccess ( ) ;
191
190
192
- try
193
- {
194
- List < Exception > exceptions = null ;
191
+ List < Exception > exceptions = null ;
195
192
196
- for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
193
+ for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
194
+ {
195
+ var circuitHandler = _circuitHandlers [ i ] ;
196
+ try
197
197
{
198
- var circuitHandler = _circuitHandlers [ i ] ;
199
- try
200
- {
201
- await circuitHandler . OnCircuitOpenedAsync ( Circuit , cancellationToken ) ;
202
- }
203
- catch ( Exception ex )
204
- {
205
- Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnCircuitOpenedAsync ) , ex ) ;
206
- exceptions ??= new List < Exception > ( ) ;
207
- exceptions . Add ( ex ) ;
208
- }
198
+ await circuitHandler . OnCircuitOpenedAsync ( Circuit , cancellationToken ) ;
209
199
}
210
-
211
- if ( exceptions != null )
200
+ catch ( Exception ex )
212
201
{
213
- throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
202
+ Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnCircuitOpenedAsync ) , ex ) ;
203
+ exceptions ??= new List < Exception > ( ) ;
204
+ exceptions . Add ( ex ) ;
214
205
}
215
206
}
216
- finally
207
+
208
+ if ( exceptions != null )
217
209
{
218
- HandlerLock . Release ( ) ;
210
+ throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
219
211
}
220
212
}
221
213
222
214
public async Task OnConnectionUpAsync ( CancellationToken cancellationToken )
223
215
{
224
216
Log . ConnectionUp ( _logger , Circuit . Id , Client . ConnectionId ) ;
225
217
226
- await HandlerLock . WaitAsync ( cancellationToken ) ;
218
+ Renderer . Dispatcher . AssertAccess ( ) ;
219
+
220
+ List < Exception > exceptions = null ;
227
221
228
- try
222
+ for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
229
223
{
230
- List < Exception > exceptions = null ;
231
-
232
- for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
224
+ var circuitHandler = _circuitHandlers [ i ] ;
225
+ try
233
226
{
234
- var circuitHandler = _circuitHandlers [ i ] ;
235
- try
236
- {
237
- await circuitHandler . OnConnectionUpAsync ( Circuit , cancellationToken ) ;
238
- }
239
- catch ( Exception ex )
240
- {
241
- Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnConnectionUpAsync ) , ex ) ;
242
- exceptions ??= new List < Exception > ( ) ;
243
- exceptions . Add ( ex ) ;
244
- }
227
+ await circuitHandler . OnConnectionUpAsync ( Circuit , cancellationToken ) ;
245
228
}
246
-
247
- if ( exceptions != null )
229
+ catch ( Exception ex )
248
230
{
249
- throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
231
+ Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnConnectionUpAsync ) , ex ) ;
232
+ exceptions ??= new List < Exception > ( ) ;
233
+ exceptions . Add ( ex ) ;
250
234
}
251
235
}
252
- finally
236
+
237
+ if ( exceptions != null )
253
238
{
254
- HandlerLock . Release ( ) ;
239
+ throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
255
240
}
256
241
}
257
242
258
243
public async Task OnConnectionDownAsync ( CancellationToken cancellationToken )
259
244
{
260
245
Log . ConnectionDown ( _logger , Circuit . Id , Client . ConnectionId ) ;
261
246
262
- await HandlerLock . WaitAsync ( cancellationToken ) ;
247
+ Renderer . Dispatcher . AssertAccess ( ) ;
248
+
249
+ List < Exception > exceptions = null ;
263
250
264
- try
251
+ for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
265
252
{
266
- List < Exception > exceptions = null ;
267
-
268
- for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
253
+ var circuitHandler = _circuitHandlers [ i ] ;
254
+ try
269
255
{
270
- var circuitHandler = _circuitHandlers [ i ] ;
271
- try
272
- {
273
- await circuitHandler . OnConnectionDownAsync ( Circuit , cancellationToken ) ;
274
- }
275
- catch ( Exception ex )
276
- {
277
- Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnConnectionDownAsync ) , ex ) ;
278
- exceptions ??= new List < Exception > ( ) ;
279
- exceptions . Add ( ex ) ;
280
- }
256
+ await circuitHandler . OnConnectionDownAsync ( Circuit , cancellationToken ) ;
281
257
}
282
-
283
- if ( exceptions != null )
258
+ catch ( Exception ex )
284
259
{
285
- throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
260
+ Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnConnectionDownAsync ) , ex ) ;
261
+ exceptions ??= new List < Exception > ( ) ;
262
+ exceptions . Add ( ex ) ;
286
263
}
287
264
}
288
- finally
265
+
266
+ if ( exceptions != null )
289
267
{
290
- HandlerLock . Release ( ) ;
268
+ throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
291
269
}
292
270
}
293
271
294
272
private async Task OnCircuitDownAsync ( CancellationToken cancellationToken )
295
273
{
296
274
Log . CircuitClosed ( _logger , Circuit . Id ) ;
297
275
298
- await HandlerLock . WaitAsync ( cancellationToken ) ;
276
+ List < Exception > exceptions = null ;
299
277
300
- try
278
+ for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
301
279
{
302
- List < Exception > exceptions = null ;
303
-
304
- for ( var i = 0 ; i < _circuitHandlers . Length ; i ++ )
280
+ var circuitHandler = _circuitHandlers [ i ] ;
281
+ try
305
282
{
306
- var circuitHandler = _circuitHandlers [ i ] ;
307
- try
308
- {
309
- await circuitHandler . OnCircuitClosedAsync ( Circuit , cancellationToken ) ;
310
- }
311
- catch ( Exception ex )
312
- {
313
- Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnCircuitClosedAsync ) , ex ) ;
314
- exceptions ??= new List < Exception > ( ) ;
315
- exceptions . Add ( ex ) ;
316
- }
283
+ await circuitHandler . OnCircuitClosedAsync ( Circuit , cancellationToken ) ;
317
284
}
318
-
319
- if ( exceptions != null )
285
+ catch ( Exception ex )
320
286
{
321
- throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
287
+ Log . CircuitHandlerFailed ( _logger , circuitHandler , nameof ( CircuitHandler . OnCircuitClosedAsync ) , ex ) ;
288
+ exceptions ??= new List < Exception > ( ) ;
289
+ exceptions . Add ( ex ) ;
322
290
}
323
291
}
324
- finally
292
+
293
+ if ( exceptions != null )
325
294
{
326
- HandlerLock . Release ( ) ;
295
+ throw new AggregateException ( "Encountered exceptions while executing circuit handlers." , exceptions ) ;
327
296
}
328
297
}
329
298
0 commit comments