14
14
15
15
namespace Microsoft . AspNetCore . Components . Server . Circuits
16
16
{
17
- internal class CircuitHost : IDisposable
17
+ internal class CircuitHost : IAsyncDisposable
18
18
{
19
19
private static readonly AsyncLocal < CircuitHost > _current = new AsyncLocal < CircuitHost > ( ) ;
20
+ private readonly IServiceScope _scope ;
21
+ private Action < IComponentsApplicationBuilder > _configure ;
22
+ private bool _isInitialized ;
20
23
21
24
/// <summary>
22
25
/// Gets the current <see cref="Circuit"/>, if any.
@@ -37,25 +40,24 @@ public static void SetCurrentCircuitHost(CircuitHost circuitHost)
37
40
{
38
41
_current . Value = circuitHost ?? throw new ArgumentNullException ( nameof ( circuitHost ) ) ;
39
42
40
- Microsoft . JSInterop . JSRuntime . SetCurrentJSRuntime ( circuitHost . JSRuntime ) ;
43
+ JSInterop . JSRuntime . SetCurrentJSRuntime ( circuitHost . JSRuntime ) ;
41
44
RendererRegistry . SetCurrentRendererRegistry ( circuitHost . RendererRegistry ) ;
42
45
}
43
46
44
47
public event UnhandledExceptionEventHandler UnhandledException ;
45
48
46
- private bool _isInitialized ;
47
- private Action < IComponentsApplicationBuilder > _configure ;
48
-
49
49
public CircuitHost (
50
50
IServiceScope scope ,
51
51
IClientProxy client ,
52
52
RendererRegistry rendererRegistry ,
53
53
RemoteRenderer renderer ,
54
54
Action < IComponentsApplicationBuilder > configure ,
55
55
IJSRuntime jsRuntime ,
56
- CircuitSynchronizationContext synchronizationContext )
56
+ CircuitSynchronizationContext synchronizationContext ,
57
+ Circuit circuit ,
58
+ CircuitHandler circuitHandler )
57
59
{
58
- Scope = scope ?? throw new ArgumentNullException ( nameof ( scope ) ) ;
60
+ _scope = scope ?? throw new ArgumentNullException ( nameof ( scope ) ) ;
59
61
Client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
60
62
RendererRegistry = rendererRegistry ?? throw new ArgumentNullException ( nameof ( rendererRegistry ) ) ;
61
63
Renderer = renderer ?? throw new ArgumentNullException ( nameof ( renderer ) ) ;
@@ -65,7 +67,8 @@ public CircuitHost(
65
67
66
68
Services = scope . ServiceProvider ;
67
69
68
- Circuit = new Circuit ( this ) ;
70
+ Circuit = circuit ;
71
+ CircuitHandler = circuitHandler ;
69
72
70
73
Renderer . UnhandledException += Renderer_UnhandledException ;
71
74
SynchronizationContext . UnhandledException += SynchronizationContext_UnhandledException ;
@@ -81,19 +84,21 @@ public CircuitHost(
81
84
82
85
public RendererRegistry RendererRegistry { get ; }
83
86
84
- public IServiceScope Scope { get ; }
85
-
86
87
public IServiceProvider Services { get ; }
87
88
88
89
public CircuitSynchronizationContext SynchronizationContext { get ; }
89
90
90
- public async Task InitializeAsync ( )
91
+ public CircuitHandler CircuitHandler { get ; }
92
+
93
+ public CancellationToken ConnectionAborted { get ; }
94
+
95
+ public async Task InitializeAsync ( CancellationToken cancellationToken )
91
96
{
92
- await SynchronizationContext . Invoke ( ( ) =>
97
+ await SynchronizationContext . InvokeAsync ( async ( ) =>
93
98
{
94
99
SetCurrentCircuitHost ( this ) ;
95
100
96
- var builder = new ServerSideBlazorApplicationBuilder ( Services ) ;
101
+ var builder = new ServerSideComponentsApplicationBuilder ( Services ) ;
97
102
98
103
_configure ( builder ) ;
99
104
@@ -102,6 +107,9 @@ await SynchronizationContext.Invoke(() =>
102
107
var entry = builder . Entries [ i ] ;
103
108
Renderer . AddComponent ( entry . componentType , entry . domElementSelector ) ;
104
109
}
110
+
111
+ await CircuitHandler . OnCircuitOpenedAsync ( Circuit , cancellationToken ) ;
112
+ await CircuitHandler . OnConnectionUpAsync ( Circuit , cancellationToken ) ;
105
113
} ) ;
106
114
107
115
_isInitialized = true ;
@@ -126,9 +134,15 @@ await SynchronizationContext.Invoke(() =>
126
134
}
127
135
}
128
136
129
- public void Dispose ( )
137
+ public async ValueTask DisposeAsync ( )
130
138
{
131
- Scope . Dispose ( ) ;
139
+ await SynchronizationContext . InvokeAsync ( async ( ) =>
140
+ {
141
+ await CircuitHandler . OnConnectionDownAsync ( Circuit , default ) ;
142
+ await CircuitHandler . OnCircuitClosedAsync ( Circuit , default ) ;
143
+ } ) ;
144
+
145
+ _scope . Dispose ( ) ;
132
146
Renderer . Dispose ( ) ;
133
147
}
134
148
0 commit comments