Invoking a method with an invalid dotnetobjectref bubbles up through the wrong path (dotnet/extensions#1940)

* Handle non existing dotnet object references and returns the error through JSInterop instead of bubbling it up to the caller.
* Every other error in JSInterop is returned that way.
* It makes sure that the error gets to the client and is sanitized appropriately if necessary.\n\nCommit migrated from a30f2cbb27
This commit is contained in:
Javier Calvarro Nelson 2019-07-05 10:16:39 +02:00 committed by GitHub
parent 9b3261722c
commit 919734cbb2
2 changed files with 23 additions and 5 deletions

View File

@ -75,19 +75,20 @@ namespace Microsoft.JSInterop
// code has to implement its own way of returning async results.
var jsRuntimeBaseInstance = (JSRuntimeBase)JSRuntime.Current;
var targetInstance = (object)null;
if (dotNetObjectId != default)
{
targetInstance = DotNetObjectRefManager.Current.FindDotNetObject(dotNetObjectId);
}
// Using ExceptionDispatchInfo here throughout because we want to always preserve
// original stack traces.
object syncResult = null;
ExceptionDispatchInfo syncException = null;
object targetInstance = null;
try
{
if (dotNetObjectId != default)
{
targetInstance = DotNetObjectRefManager.Current.FindDotNetObject(dotNetObjectId);
}
syncResult = InvokeSynchronously(assemblyName, methodIdentifier, targetInstance, argsJson);
}
catch (Exception ex)

View File

@ -381,6 +381,23 @@ namespace Microsoft.JSInterop.Tests
Assert.Contains("JsonReaderException: '<' is an invalid start of a value.", result[2].GetString());
});
[Fact]
public Task BeginInvoke_ThrowsWithInvalid_DotNetObjectRef() => WithJSRuntime(jsRuntime =>
{
// Arrange
var callId = "123";
var resultTask = jsRuntime.NextInvocationTask;
DotNetDispatcher.BeginInvoke(callId, null, "InvokableInstanceVoid", 1, null);
// Assert
using var jsonDocument = JsonDocument.Parse(jsRuntime.LastInvocationArgsJson);
var result = jsonDocument.RootElement;
Assert.Equal(callId, result[0].GetString());
Assert.False(result[1].GetBoolean()); // Fails
Assert.StartsWith("System.ArgumentException: There is no tracked object with id '1'. Perhaps the DotNetObjectRef instance was already disposed.", result[2].GetString());
});
Task WithJSRuntime(Action<TestJSRuntime> testCode)
{
return WithJSRuntime(jsRuntime =>