@@ -27,12 +27,13 @@ public static class DotNetDispatcher
27
27
/// <summary>
28
28
/// Receives a call from JS to .NET, locating and invoking the specified method.
29
29
/// </summary>
30
+ /// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
30
31
/// <param name="assemblyName">The assembly containing the method to be invoked.</param>
31
32
/// <param name="methodIdentifier">The identifier of the method to be invoked. The method must be annotated with a <see cref="JSInvokableAttribute"/> matching this identifier string.</param>
32
33
/// <param name="dotNetObjectId">For instance method calls, identifies the target object.</param>
33
34
/// <param name="argsJson">A JSON representation of the parameters.</param>
34
35
/// <returns>A JSON representation of the return value, or null.</returns>
35
- public static string Invoke ( string assemblyName , string methodIdentifier , long dotNetObjectId , string argsJson )
36
+ public static string Invoke ( JSRuntime jsRuntime , string assemblyName , string methodIdentifier , long dotNetObjectId , string argsJson )
36
37
{
37
38
// This method doesn't need [JSInvokable] because the platform is responsible for having
38
39
// some way to dispatch calls here. The logic inside here is the thing that checks whether
@@ -42,41 +43,38 @@ public static string Invoke(string assemblyName, string methodIdentifier, long d
42
43
IDotNetObjectReference targetInstance = default ;
43
44
if ( dotNetObjectId != default )
44
45
{
45
- targetInstance = DotNetObjectReferenceManager . Current . FindDotNetObject ( dotNetObjectId ) ;
46
+ targetInstance = jsRuntime . GetObjectReference ( dotNetObjectId ) ;
46
47
}
47
48
48
- var syncResult = InvokeSynchronously ( assemblyName , methodIdentifier , targetInstance , argsJson ) ;
49
+ var syncResult = InvokeSynchronously ( jsRuntime , assemblyName , methodIdentifier , targetInstance , argsJson ) ;
49
50
if ( syncResult == null )
50
51
{
51
52
return null ;
52
53
}
53
54
54
- return JsonSerializer . Serialize ( syncResult , JsonSerializerOptionsProvider . Options ) ;
55
+ return JsonSerializer . Serialize ( syncResult , jsRuntime . JsonSerializerOptions ) ;
55
56
}
56
57
57
58
/// <summary>
58
59
/// Receives a call from JS to .NET, locating and invoking the specified method asynchronously.
59
60
/// </summary>
61
+ /// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
60
62
/// <param name="callId">A value identifying the asynchronous call that should be passed back with the result, or null if no result notification is required.</param>
61
63
/// <param name="assemblyName">The assembly containing the method to be invoked.</param>
62
64
/// <param name="methodIdentifier">The identifier of the method to be invoked. The method must be annotated with a <see cref="JSInvokableAttribute"/> matching this identifier string.</param>
63
65
/// <param name="dotNetObjectId">For instance method calls, identifies the target object.</param>
64
66
/// <param name="argsJson">A JSON representation of the parameters.</param>
65
67
/// <returns>A JSON representation of the return value, or null.</returns>
66
- public static void BeginInvokeDotNet ( string callId , string assemblyName , string methodIdentifier , long dotNetObjectId , string argsJson )
68
+ public static void BeginInvokeDotNet ( JSRuntime jsRuntime , string callId , string assemblyName , string methodIdentifier , long dotNetObjectId , string argsJson )
67
69
{
68
70
// This method doesn't need [JSInvokable] because the platform is responsible for having
69
71
// some way to dispatch calls here. The logic inside here is the thing that checks whether
70
72
// the targeted method has [JSInvokable]. It is not itself subject to that restriction,
71
73
// because there would be nobody to police that. This method *is* the police.
72
74
73
- // DotNetDispatcher only works with JSRuntimeBase instances.
74
- // If the developer wants to use a totally custom IJSRuntime, then their JS-side
75
- // code has to implement its own way of returning async results.
76
- var jsRuntimeBaseInstance = ( JSRuntime ) JSRuntime . Current ;
77
-
78
75
// Using ExceptionDispatchInfo here throughout because we want to always preserve
79
76
// original stack traces.
77
+
80
78
object syncResult = null ;
81
79
ExceptionDispatchInfo syncException = null ;
82
80
IDotNetObjectReference targetInstance = null ;
@@ -85,10 +83,10 @@ public static void BeginInvokeDotNet(string callId, string assemblyName, string
85
83
{
86
84
if ( dotNetObjectId != default )
87
85
{
88
- targetInstance = DotNetObjectReferenceManager . Current . FindDotNetObject ( dotNetObjectId ) ;
86
+ targetInstance = jsRuntime . GetObjectReference ( dotNetObjectId ) ;
89
87
}
90
88
91
- syncResult = InvokeSynchronously ( assemblyName , methodIdentifier , targetInstance , argsJson ) ;
89
+ syncResult = InvokeSynchronously ( jsRuntime , assemblyName , methodIdentifier , targetInstance , argsJson ) ;
92
90
}
93
91
catch ( Exception ex )
94
92
{
@@ -103,7 +101,7 @@ public static void BeginInvokeDotNet(string callId, string assemblyName, string
103
101
else if ( syncException != null )
104
102
{
105
103
// Threw synchronously, let's respond.
106
- jsRuntimeBaseInstance . EndInvokeDotNet ( callId , false , syncException , assemblyName , methodIdentifier , dotNetObjectId ) ;
104
+ jsRuntime . EndInvokeDotNet ( callId , false , syncException , assemblyName , methodIdentifier , dotNetObjectId ) ;
107
105
}
108
106
else if ( syncResult is Task task )
109
107
{
@@ -115,20 +113,20 @@ public static void BeginInvokeDotNet(string callId, string assemblyName, string
115
113
{
116
114
var exception = t . Exception . GetBaseException ( ) ;
117
115
118
- jsRuntimeBaseInstance . EndInvokeDotNet ( callId , false , ExceptionDispatchInfo . Capture ( exception ) , assemblyName , methodIdentifier , dotNetObjectId ) ;
116
+ jsRuntime . EndInvokeDotNet ( callId , false , ExceptionDispatchInfo . Capture ( exception ) , assemblyName , methodIdentifier , dotNetObjectId ) ;
119
117
}
120
118
121
119
var result = TaskGenericsUtil . GetTaskResult ( task ) ;
122
- jsRuntimeBaseInstance . EndInvokeDotNet ( callId , true , result , assemblyName , methodIdentifier , dotNetObjectId ) ;
120
+ jsRuntime . EndInvokeDotNet ( callId , true , result , assemblyName , methodIdentifier , dotNetObjectId ) ;
123
121
} , TaskScheduler . Current ) ;
124
122
}
125
123
else
126
124
{
127
- jsRuntimeBaseInstance . EndInvokeDotNet ( callId , true , syncResult , assemblyName , methodIdentifier , dotNetObjectId ) ;
125
+ jsRuntime . EndInvokeDotNet ( callId , true , syncResult , assemblyName , methodIdentifier , dotNetObjectId ) ;
128
126
}
129
127
}
130
128
131
- private static object InvokeSynchronously ( string assemblyName , string methodIdentifier , IDotNetObjectReference objectReference , string argsJson )
129
+ private static object InvokeSynchronously ( JSRuntime jsRuntime , string assemblyName , string methodIdentifier , IDotNetObjectReference objectReference , string argsJson )
132
130
{
133
131
AssemblyKey assemblyKey ;
134
132
if ( objectReference is null )
@@ -154,7 +152,7 @@ private static object InvokeSynchronously(string assemblyName, string methodIden
154
152
155
153
var ( methodInfo , parameterTypes ) = GetCachedMethodInfo ( assemblyKey , methodIdentifier ) ;
156
154
157
- var suppliedArgs = ParseArguments ( methodIdentifier , argsJson , parameterTypes ) ;
155
+ var suppliedArgs = ParseArguments ( jsRuntime , methodIdentifier , argsJson , parameterTypes ) ;
158
156
159
157
try
160
158
{
@@ -173,7 +171,7 @@ private static object InvokeSynchronously(string assemblyName, string methodIden
173
171
}
174
172
}
175
173
176
- internal static object [ ] ParseArguments ( string methodIdentifier , string arguments , Type [ ] parameterTypes )
174
+ internal static object [ ] ParseArguments ( JSRuntime jsRuntime , string methodIdentifier , string arguments , Type [ ] parameterTypes )
177
175
{
178
176
if ( parameterTypes . Length == 0 )
179
177
{
@@ -198,7 +196,7 @@ internal static object[] ParseArguments(string methodIdentifier, string argument
198
196
throw new InvalidOperationException ( $ "In call to '{ methodIdentifier } ', parameter of type '{ parameterType . Name } ' at index { ( index + 1 ) } must be declared as type 'DotNetObjectRef<{ parameterType . Name } >' to receive the incoming value.") ;
199
197
}
200
198
201
- suppliedArgs [ index ] = JsonSerializer . Deserialize ( ref reader , parameterType , JsonSerializerOptionsProvider . Options ) ;
199
+ suppliedArgs [ index ] = JsonSerializer . Deserialize ( ref reader , parameterType , jsRuntime . JsonSerializerOptions ) ;
202
200
index ++ ;
203
201
}
204
202
@@ -247,18 +245,13 @@ static bool IsIncorrectDotNetObjectRefUse(Type parameterType, Utf8JsonReader jso
247
245
/// method is responsible for handling any possible exception generated from the arguments
248
246
/// passed in as parameters.
249
247
/// </remarks>
248
+ /// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
250
249
/// <param name="arguments">The serialized arguments for the callback completion.</param>
251
250
/// <exception cref="Exception">
252
251
/// This method can throw any exception either from the argument received or as a result
253
252
/// of executing any callback synchronously upon completion.
254
253
/// </exception>
255
- public static void EndInvokeJS ( string arguments )
256
- {
257
- var jsRuntimeBase = ( JSRuntime ) JSRuntime . Current ;
258
- ParseEndInvokeArguments ( jsRuntimeBase , arguments ) ;
259
- }
260
-
261
- internal static void ParseEndInvokeArguments ( JSRuntime jsRuntimeBase , string arguments )
254
+ public static void EndInvokeJS ( JSRuntime jsRuntime , string arguments )
262
255
{
263
256
var utf8JsonBytes = Encoding . UTF8 . GetBytes ( arguments ) ;
264
257
@@ -281,7 +274,7 @@ internal static void ParseEndInvokeArguments(JSRuntime jsRuntimeBase, string arg
281
274
var success = reader . GetBoolean ( ) ;
282
275
283
276
reader . Read ( ) ;
284
- jsRuntimeBase . EndInvokeJS ( taskId , success , ref reader ) ;
277
+ jsRuntime . EndInvokeJS ( taskId , success , ref reader ) ;
285
278
286
279
if ( ! reader . Read ( ) || reader . TokenType != JsonTokenType . EndArray )
287
280
{
0 commit comments