@@ -53,14 +53,14 @@ public sealed class Broker : IAsyncDisposable
5353 private Task ? _eventEmitterTask ;
5454 private CancellationTokenSource ? _receiveMessagesCancellationTokenSource ;
5555
56- private readonly BiDiJsonSerializerContext _jsonSerializerContext ;
56+ private readonly JsonSerializerOptions _jsonSerializerOptions ;
5757
5858 internal Broker ( BiDi bidi , Uri url )
5959 {
6060 _bidi = bidi ;
6161 _transport = new WebSocketTransport ( url ) ;
6262
63- var jsonSerializerOptions = new JsonSerializerOptions
63+ _jsonSerializerOptions = new JsonSerializerOptions
6464 {
6565 PropertyNameCaseInsensitive = true ,
6666 PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
@@ -108,7 +108,14 @@ internal Broker(BiDi bidi, Uri url)
108108 }
109109 } ;
110110
111- _jsonSerializerContext = new BiDiJsonSerializerContext ( jsonSerializerOptions ) ;
111+ // Add base BiDi generated context resolver; keep options mutable for module contexts
112+ _jsonSerializerOptions . TypeInfoResolverChain . Add ( BiDiJsonSerializerContext . Default ) ;
113+ }
114+
115+ public void ConfigureJsonContext ( Action < JsonSerializerOptions > action )
116+ {
117+ // Keep options mutable; do not create a context bound to them (avoids InvalidOperationException)
118+ action ( _jsonSerializerOptions ) ;
112119 }
113120
114121 public async Task ConnectAsync ( CancellationToken cancellationToken )
@@ -205,21 +212,14 @@ private async Task<EmptyResult> ExecuteCommandCoreAsync<TCommand>(TCommand comma
205212 where TCommand : Command
206213 {
207214 command . Id = Interlocked . Increment ( ref _currentCommandId ) ;
208-
209215 var tcs = new TaskCompletionSource < EmptyResult > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
210-
211216 var timeout = options ? . Timeout ?? TimeSpan . FromSeconds ( 30 ) ;
212-
213217 using var cts = new CancellationTokenSource ( timeout ) ;
214-
215218 cts . Token . Register ( ( ) => tcs . TrySetCanceled ( cts . Token ) ) ;
216-
217219 _pendingCommands [ command . Id ] = new ( command . Id , command . ResultType , tcs ) ;
218220
219- var data = JsonSerializer . SerializeToUtf8Bytes ( command , typeof ( TCommand ) , _jsonSerializerContext ) ;
220-
221+ var data = JsonSerializer . SerializeToUtf8Bytes ( command , typeof ( TCommand ) , _jsonSerializerOptions ) ;
221222 await _transport . SendAsync ( data , cts . Token ) . ConfigureAwait ( false ) ;
222-
223223 return await tcs . Task . ConfigureAwait ( false ) ;
224224 }
225225
@@ -341,11 +341,11 @@ private void ProcessReceivedMessage(byte[]? data)
341341 break ;
342342
343343 case "result" :
344- resultReader = reader ; // cloning reader with current position
344+ resultReader = reader ; // snapshot
345345 break ;
346346
347347 case "params" :
348- paramsReader = reader ; // cloning reader with current position
348+ paramsReader = reader ; // snapshot
349349 break ;
350350
351351 case "error" :
@@ -368,7 +368,7 @@ private void ProcessReceivedMessage(byte[]? data)
368368
369369 if ( _pendingCommands . TryGetValue ( id . Value , out var successCommand ) )
370370 {
371- var messageSuccess = JsonSerializer . Deserialize ( ref resultReader , successCommand . ResultType , _jsonSerializerContext ) ! ;
371+ var messageSuccess = JsonSerializer . Deserialize ( ref resultReader , successCommand . ResultType , _jsonSerializerOptions ) ! ;
372372 successCommand . TaskCompletionSource . SetResult ( ( EmptyResult ) messageSuccess ) ;
373373 _pendingCommands . TryRemove ( id . Value , out _ ) ;
374374 }
@@ -384,7 +384,7 @@ private void ProcessReceivedMessage(byte[]? data)
384384
385385 if ( _eventTypesMap . TryGetValue ( method , out var eventType ) )
386386 {
387- var eventArgs = ( EventArgs ) JsonSerializer . Deserialize ( ref paramsReader , eventType , _jsonSerializerContext ) ! ;
387+ var eventArgs = ( EventArgs ) JsonSerializer . Deserialize ( ref paramsReader , eventType , _jsonSerializerOptions ) ! ;
388388
389389 var messageEvent = new MessageEvent ( method , eventArgs ) ;
390390 _pendingEvents . Add ( messageEvent ) ;
0 commit comments