@using Microsoft.JSInterop @using BasicTestApp.InteropTest @using System.Runtime.InteropServices

Invocations

@foreach (var invocation in Invocations) {

@invocation.Key

@invocation.Value

}

.NET to JS calls: passing .NET object by ref, receiving .NET object by ref

@foreach (var kvp in ReceiveDotNetObjectByRefResult) {

@(kvp.Key)Sync

@kvp.Value

} @foreach (var kvp in ReceiveDotNetObjectByRefAsyncResult) {

@(kvp.Key)Async

@kvp.Value

}

Return values and exceptions thrown from .NET

@foreach (var returnValue in ReturnValues) {

@returnValue.Key

@returnValue.Value

}

Exceptions thrown from JavaScript

@nameof(ExceptionFromSyncMethod)

@ExceptionFromSyncMethod?.Message

@nameof(SyncExceptionFromAsyncMethod)

@SyncExceptionFromAsyncMethod?.Message

@nameof(AsyncExceptionFromAsyncMethod)

@AsyncExceptionFromAsyncMethod?.Message

@if (DoneWithInterop) {

Done with interop.

} @functions { public IDictionary ReturnValues { get; set; } = new Dictionary(); public IDictionary Invocations { get; set; } = new Dictionary(); public JSException ExceptionFromSyncMethod { get; set; } public JSException SyncExceptionFromAsyncMethod { get; set; } public JSException AsyncExceptionFromAsyncMethod { get; set; } public IDictionary ReceiveDotNetObjectByRefResult { get; set; } = new Dictionary(); public IDictionary ReceiveDotNetObjectByRefAsyncResult { get; set; } = new Dictionary(); public bool DoneWithInterop { get; set; } public async Task InvokeInteropAsync() { var shouldSupportSyncInterop = RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY")); var testDTOTOPassByRef = new TestDTO(nonSerializedValue: 123); var instanceMethodsTarget = new JavaScriptInterop(); Console.WriteLine("Starting interop invocations."); await JSRuntime.Current.InvokeAsync( "jsInteropTests.invokeDotNetInteropMethodsAsync", shouldSupportSyncInterop, new DotNetObjectRef(testDTOTOPassByRef), new DotNetObjectRef(instanceMethodsTarget)); if (shouldSupportSyncInterop) { InvokeInProcessInterop(); } Console.WriteLine("Showing interop invocation results."); var collectResults = await JSRuntime.Current.InvokeAsync>("jsInteropTests.collectInteropResults"); ReturnValues = collectResults.ToDictionary(kvp => kvp.Key,kvp => System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(kvp.Value))); var invocations = new Dictionary(); foreach (var interopResult in JavaScriptInterop.Invocations) { var interopResultValue = Json.Serialize(interopResult.Value); invocations[interopResult.Key] = interopResultValue; } try { await JSRuntime.Current.InvokeAsync("jsInteropTests.asyncFunctionThrowsSyncException"); } catch (JSException e) { SyncExceptionFromAsyncMethod = e; } try { await JSRuntime.Current.InvokeAsync("jsInteropTests.asyncFunctionThrowsAsyncException"); } catch (JSException e) { AsyncExceptionFromAsyncMethod = e; } var passDotNetObjectByRef = new TestDTO(99999); var passDotNetObjectByRefArg = new Dictionary { { "stringValue", "My string" }, { "testDto", new DotNetObjectRef(passDotNetObjectByRef) }, }; ReceiveDotNetObjectByRefAsyncResult = await JSRuntime.Current.InvokeAsync>("receiveDotNetObjectByRefAsync", passDotNetObjectByRefArg); ReceiveDotNetObjectByRefAsyncResult["testDto"] = ReceiveDotNetObjectByRefAsyncResult["testDto"] == passDotNetObjectByRef ? "Same" : "Different"; ReturnValues["returnPrimitiveAsync"] = (await JSRuntime.Current.InvokeAsync("returnPrimitiveAsync")).ToString(); ReturnValues["returnArrayAsync"] = string.Join(",", (await JSRuntime.Current.InvokeAsync("returnArrayAsync")).Select(x => x.Source).ToArray()); if (shouldSupportSyncInterop) { ReturnValues["returnPrimitive"] = ((IJSInProcessRuntime)JSRuntime.Current).Invoke("returnPrimitive").ToString(); ReturnValues["returnArray"] = string.Join(",", ((IJSInProcessRuntime)JSRuntime.Current).Invoke("returnArray").Select(x => x.Source).ToArray()); } Invocations = invocations; DoneWithInterop = true; } public void InvokeInProcessInterop() { var inProcRuntime = ((IJSInProcessRuntime)JSRuntime.Current); try { inProcRuntime.Invoke("jsInteropTests.functionThrowsException"); } catch (JSException e) { ExceptionFromSyncMethod = e; } var passDotNetObjectByRef = new TestDTO(99999); var passDotNetObjectByRefArg = new Dictionary { { "stringValue", "My string" }, { "testDto", new DotNetObjectRef(passDotNetObjectByRef) }, }; ReceiveDotNetObjectByRefResult = inProcRuntime.Invoke>("receiveDotNetObjectByRef", passDotNetObjectByRefArg); ReceiveDotNetObjectByRefResult["testDto"] = ReceiveDotNetObjectByRefResult["testDto"] == passDotNetObjectByRef ? "Same" : "Different"; } }