2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Runtime . CompilerServices ;
5
6
using System . Threading . Tasks ;
6
7
using Microsoft . AspNetCore . Components . Server . Circuits ;
7
8
using Microsoft . AspNetCore . Http ;
@@ -73,6 +74,13 @@ public override Task OnDisconnectedAsync(Exception exception)
73
74
/// </summary>
74
75
public string StartCircuit ( string uriAbsolute , string baseUriAbsolute )
75
76
{
77
+ if ( CircuitHost != null )
78
+ {
79
+ Log . CircuitAlreadyInitialized ( _logger , CircuitHost . CircuitId ) ;
80
+ NotifyClientError ( Clients . Caller , $ "The circuit host '{ CircuitHost . CircuitId } ' has already been initialized.") ;
81
+ return null ;
82
+ }
83
+
76
84
var circuitClient = new CircuitClientProxy ( Clients . Caller , Context . ConnectionId ) ;
77
85
if ( DefaultCircuitFactory . ResolveComponentMetadata ( Context . GetHttpContext ( ) , circuitClient ) . Count == 0 )
78
86
{
@@ -129,26 +137,60 @@ public async Task<bool> ConnectCircuit(string circuitId)
129
137
/// </summary>
130
138
public void BeginInvokeDotNetFromJS ( string callId , string assemblyName , string methodIdentifier , long dotNetObjectId , string argsJson )
131
139
{
132
- _ = EnsureCircuitHost ( ) . BeginInvokeDotNetFromJS ( callId , assemblyName , methodIdentifier , dotNetObjectId , argsJson ) ;
140
+ if ( CircuitHost == null )
141
+ {
142
+ Log . CircuitHostNotInitialized ( _logger ) ;
143
+ _ = NotifyClientError ( Clients . Caller , "Circuit not initialized." ) ;
144
+ return ;
145
+ }
146
+
147
+ _ = CircuitHost . BeginInvokeDotNetFromJS ( callId , assemblyName , methodIdentifier , dotNetObjectId , argsJson ) ;
133
148
}
134
149
150
+ /// <summary>
151
+ /// Intended for framework use only. Applications should not call this method directly.
152
+ /// </summary>
135
153
public void EndInvokeJSFromDotNet ( long asyncHandle , bool succeeded , string arguments )
136
154
{
137
- _ = EnsureCircuitHost ( ) . EndInvokeJSFromDotNet ( asyncHandle , succeeded , arguments ) ;
155
+ if ( CircuitHost == null )
156
+ {
157
+ Log . CircuitHostNotInitialized ( _logger ) ;
158
+ _ = NotifyClientError ( Clients . Caller , "Circuit not initialized." ) ;
159
+ return ;
160
+ }
161
+
162
+ _ = CircuitHost . EndInvokeJSFromDotNet ( asyncHandle , succeeded , arguments ) ;
138
163
}
139
164
165
+ /// <summary>
166
+ /// Intended for framework use only. Applications should not call this method directly.
167
+ /// </summary>
140
168
public void DispatchBrowserEvent ( string eventDescriptor , string eventArgs )
141
169
{
142
- _ = EnsureCircuitHost ( ) . DispatchEvent ( eventDescriptor , eventArgs ) ;
170
+ if ( CircuitHost == null )
171
+ {
172
+ Log . CircuitHostNotInitialized ( _logger ) ;
173
+ _ = NotifyClientError ( Clients . Caller , "Circuit not initialized." ) ;
174
+ return ;
175
+ }
176
+
177
+ _ = CircuitHost . DispatchEvent ( eventDescriptor , eventArgs ) ;
143
178
}
144
179
145
180
/// <summary>
146
181
/// Intended for framework use only. Applications should not call this method directly.
147
182
/// </summary>
148
183
public void OnRenderCompleted ( long renderId , string errorMessageOrNull )
149
184
{
185
+ if ( CircuitHost == null )
186
+ {
187
+ Log . CircuitHostNotInitialized ( _logger ) ;
188
+ NotifyClientError ( Clients . Caller , "Circuit not initialized." ) ;
189
+ return ;
190
+ }
191
+
150
192
Log . ReceivedConfirmationForBatch ( _logger , renderId ) ;
151
- EnsureCircuitHost ( ) . Renderer . OnRenderCompleted ( renderId , errorMessageOrNull ) ;
193
+ CircuitHost . Renderer . OnRenderCompleted ( renderId , errorMessageOrNull ) ;
152
194
}
153
195
154
196
private async void CircuitHost_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
@@ -161,14 +203,14 @@ private async void CircuitHost_UnhandledException(object sender, UnhandledExcept
161
203
Log . UnhandledExceptionInCircuit ( _logger , circuitId , ( Exception ) e . ExceptionObject ) ;
162
204
if ( _options . DetailedErrors )
163
205
{
164
- await circuitHost . Client . SendAsync ( "JS.Error" , e . ExceptionObject . ToString ( ) ) ;
206
+ await NotifyClientError ( circuitHost . Client , e . ExceptionObject . ToString ( ) ) ;
165
207
}
166
208
else
167
209
{
168
210
var message = $ "There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on " +
169
211
$ "detailed exceptions in '{ typeof ( CircuitOptions ) . Name } .{ nameof ( CircuitOptions . DetailedErrors ) } '";
170
212
171
- await circuitHost . Client . SendAsync ( "JS.Error" , message ) ;
213
+ await NotifyClientError ( circuitHost . Client , message ) ;
172
214
}
173
215
174
216
// We generally can't abort the connection here since this is an async
@@ -181,17 +223,8 @@ private async void CircuitHost_UnhandledException(object sender, UnhandledExcept
181
223
}
182
224
}
183
225
184
- private CircuitHost EnsureCircuitHost ( )
185
- {
186
- var circuitHost = CircuitHost ;
187
- if ( circuitHost == null )
188
- {
189
- var message = $ "The { nameof ( CircuitHost ) } is null. This is due to an exception thrown during initialization.";
190
- throw new InvalidOperationException ( message ) ;
191
- }
192
-
193
- return circuitHost ;
194
- }
226
+ private static Task NotifyClientError ( IClientProxy client , string error ) =>
227
+ client . SendAsync ( "JS.Error" , error ) ;
195
228
196
229
private static class Log
197
230
{
@@ -207,6 +240,13 @@ private static class Log
207
240
private static readonly Action < ILogger , string , Exception > _failedToTransmitException =
208
241
LoggerMessage . Define < string > ( LogLevel . Debug , new EventId ( 4 , "FailedToTransmitException" ) , "Failed to transmit exception to client in circuit {CircuitId}" ) ;
209
242
243
+ private static readonly Action < ILogger , string , Exception > _circuitAlreadyInitialized =
244
+ LoggerMessage . Define < string > ( LogLevel . Debug , new EventId ( 5 , "CircuitAlreadyInitialized" ) , "The circuit host '{CircuitId}' has already been initialized" ) ;
245
+
246
+ private static readonly Action < ILogger , string , Exception > _circuitHostNotInitialized =
247
+ LoggerMessage . Define < string > ( LogLevel . Debug , new EventId ( 6 , "CircuitHostNotInitialized" ) , "Call to '{CallSite}' received before the circuit host initialization." ) ;
248
+
249
+
210
250
public static void NoComponentsRegisteredInEndpoint ( ILogger logger , string endpointDisplayName )
211
251
{
212
252
_noComponentsRegisteredInEndpoint ( logger , endpointDisplayName , null ) ;
@@ -226,6 +266,10 @@ public static void FailedToTransmitException(ILogger logger, string circuitId, E
226
266
{
227
267
_failedToTransmitException ( logger , circuitId , transmissionException ) ;
228
268
}
269
+
270
+ public static void CircuitAlreadyInitialized ( ILogger logger , string circuitId ) => _circuitAlreadyInitialized ( logger , circuitId , null ) ;
271
+
272
+ public static void CircuitHostNotInitialized ( ILogger logger , [ CallerMemberName ] string callSite = "" ) => _circuitHostNotInitialized ( logger , callSite , null ) ;
229
273
}
230
274
}
231
275
}
0 commit comments