Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public static class DotNetDispatcher
return null;
}

// When an async [JSInvokable] method is called synchronously via invokeMethod(), the raw

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the comment, the code is self-explainatory.

// Task/ValueTask is returned and serialization produces misleading JSON errors. Detect this
// and throw a clear exception instead.
if (syncResult is Task || syncResult is ValueTask)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (syncResult is Task || syncResult is ValueTask)
if (result is Task or ValueTask
|| (result?.GetType() is { IsGenericType: true } resultType
&& resultType.GetGenericTypeDefinition() == typeof(ValueTask<>)))

We can shorten it to one condition.

{
throw new InvalidOperationException(
$"The method '{invocationInfo.MethodIdentifier}' returns an asynchronous type and cannot be invoked " +
$"synchronously from JavaScript. Use 'invokeMethodAsync' instead of 'invokeMethod'.");
}

var resultType = syncResult.GetType();
if (resultType.IsValueType && resultType.IsGenericType && resultType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
throw new InvalidOperationException(
$"The method '{invocationInfo.MethodIdentifier}' returns an asynchronous type and cannot be invoked " +
$"synchronously from JavaScript. Use 'invokeMethodAsync' instead of 'invokeMethod'.");
}

Comment thread
iamcymentho marked this conversation as resolved.
Resolved
return JsonSerializer.Serialize(syncResult, jsRuntime.JsonSerializerOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,44 @@ public void CannotInvokeWithMoreParameters()
Assert.Equal("Unexpected JSON token Number. Ensure that the call to `InvocableStaticWithParams' is supplied with exactly '3' parameters.", ex.Message);
}

[Fact]
public void SyncInvokeOfAsyncMethod_Task_ThrowsMeaningfulException()
{
// Arrange: Track an instance with an async method
var jsRuntime = new TestJSRuntime();
var targetInstance = new SomePublicType();
var objectRef = DotNetObjectReference.Create(targetInstance);
jsRuntime.Invoke<object>("unimportant", objectRef);

// Act & Assert: Synchronously invoking an async method should throw a clear error
var ex = Assert.Throws<InvalidOperationException>(() =>
{
DotNetDispatcher.Invoke(jsRuntime, new DotNetInvocationInfo(null, nameof(SomePublicType.InvokableAsyncReturningTask), 1, default), null);
});
Comment thread
iamcymentho marked this conversation as resolved.
Resolved

Assert.Contains("invokeMethodAsync", ex.Message);
Assert.Contains("InvokableAsyncReturningTask", ex.Message);
}

[Fact]
public void SyncInvokeOfAsyncMethod_ValueTask_ThrowsMeaningfulException()
{
// Arrange: Track an instance with an async method returning ValueTask
var jsRuntime = new TestJSRuntime();
var targetInstance = new SomePublicType();
var objectRef = DotNetObjectReference.Create(targetInstance);
jsRuntime.Invoke<object>("unimportant", objectRef);

// Act & Assert: Synchronously invoking an async method should throw a clear error
var ex = Assert.Throws<InvalidOperationException>(() =>
{
DotNetDispatcher.Invoke(jsRuntime, new DotNetInvocationInfo(null, nameof(SomePublicType.InvokableAsyncMethodReturningValueTaskNonGeneric), 1, default), null);
});
Comment thread
iamcymentho marked this conversation as resolved.
Resolved

Assert.Contains("invokeMethodAsync", ex.Message);
Assert.Contains("InvokableAsyncMethodReturningValueTaskNonGeneric", ex.Message);
}

[Fact]
public async Task CanInvokeAsyncMethod()
{
Expand Down Expand Up @@ -1001,6 +1039,12 @@ public async ValueTask<InvokableAsyncMethodResult> InvokableAsyncMethodReturning
});
}

[JSInvokable]
public async Task InvokableAsyncReturningTask()
{
await Task.CompletedTask;
}
Comment thread
iamcymentho marked this conversation as resolved.
Resolved

[JSInvokable]
public async ValueTask InvokableAsyncMethodReturningValueTaskNonGeneric()
{
Expand Down
Loading