private async Task <int> ReadFileUnmarshalledAsync( int fileRef, byte[] buffer, long position, long bufferOffset, int count, CancellationToken cancellationToken) { var taskCompletionSource = new TaskCompletionSource <int>(); var id = ++_readFileUnmarshalledCallIdSource; _readFileUnmarshalledCalls[id] = taskCompletionSource; cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()); // Do not try to push over the buffer here... CurrentJSRuntime.InvokeUnmarshalled <ReadFileParams, int>( $"FileReaderComponent.ReadFileUnmarshalledAsync", new ReadFileParams { BufferOffset = bufferOffset, Count = count, FileRef = fileRef, Position = position, TaskId = id }); // as it might not survive the heap charge of the following statement await taskCompletionSource.Task; // Charge the buffer here instead, once its loadeg on the js side var bytesRead = CurrentJSRuntime.InvokeUnmarshalled <BufferParams, int>( $"FileReaderComponent.FillBufferUnmarshalled", new BufferParams { TaskId = id, Buffer = buffer }); return(bytesRead); }