As a corollary to Need C# JSImport async signature interop example to match async Javascript method - Blazor
I'd like to find a Span/ArraySegment solution to passing and receiving parameters via javascript interop in Blazor.
Here's my best attempt so far, with syntax I would expect to work:
The import
[JSImport("getMessage", "SampleJS")]
[return: JSMarshalAs<JSType.Promise<JSType.Any>>()]
internal static partial Task<object>
GetWelcomeMessage([JSMarshalAs<JSType.MemoryView>] ArraySegment<byte> bytes);
JS module
export async function getMessage(dataPointer) {
var maskedData = new Uint8Array(dataPointer) // this just creates a zero-filled array
console.log(maskedData)
return await new Promise((resolve) => {
setTimeout(() => {
resolve(maskedData);
}, 2000);
});
}
This just logs the data (bytes) received, waits 2s and returns the same data.
Usage
byte[] sampleData = Encoding.UTF8.GetBytes("Hello from C#");
ArraySegment<byte> sampleDataPointer = new ArraySegment<byte>(sampleData);
object? result = await GetWelcomeMessage(sampleDataPointer);
if (result is byte[] bytes)
{
message = Encoding.UTF8.GetString(bytes);
Console.WriteLine($"Got {message} from {result.GetType()}");
}
So the problem is in the javascript method. I can't turn the javascript's incoming MemoryView parameter into a Uint8Array so I can work with it. Any suggestions?
Many thanks to Mister Magoo for getting this off the ground