Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Blazor preview 9/mono-wasm memory access out of bounds: max string size for DotNet.invokeMethod?

Ask Question

Since dotnet core 3 preview 9, I am facing an issue invoking a dotnet method passing a large string from JavaScript.

Code is worth more than a thousand words, so the snippet below reproduces the issue. It works when length = 1 * mb but fails when length = 2 * mb .

@page "/repro"
<button onclick="const mb = 1024 * 1024; const length = 2 * mb;console.log(`Attempting length ${length}`); DotNet.invokeMethod('@GetType().Assembly.GetName().Name', 'ProcessString', 'a'.repeat(length));">Click Me</button>
@functions {
    [JSInvokable] public static void ProcessString(string stringFromJavaScript) { }

The error message is:

Uncaught RuntimeError: memory access out of bounds
    at wasm-function[2639]:18
    at wasm-function[6239]:10
    at Module._mono_wasm_string_from_js (http://localhost:52349/_framework/wasm/mono.js:1:202444)
    at ccall (http://localhost:52349/_framework/wasm/mono.js:1:7888)
    at http://localhost:52349/_framework/wasm/mono.js:1:8238
    at Object.toDotNetString (http://localhost:52349/_framework/blazor.webassembly.js:1:39050)
    at Object.invokeDotNetFromJS (http://localhost:52349/_framework/blazor.webassembly.js:1:37750)
    at u (http://localhost:52349/_framework/blazor.webassembly.js:1:5228)
    at Object.e.invokeMethod (http://localhost:52349/_framework/blazor.webassembly.js:1:6578)
    at HTMLButtonElement.onclick (<anonymous>:2:98)

I need to process large strings, which represent the content of a file.

  • Is there a way to increase this limit?
  • Apart from breaking down the string into multiple segments and performing multiples calls, is there any other way to process a large string?
  • Is there any other approach for processing large files?
  • This used to work in preview 8.

    Thank you @Isaac. That was one of the few related things I found doing my research before asking the question. It doesn't seem to be related as the link is about max memory whereas this issue seems to be specifically about the marshalling of a string or object. Notice that 1 million chars work but 2 doesn't. – Rodolfo Grave Sep 16, 2019 at 7:54
  • Apart from breaking down the string into multiple segments and performing multiples calls, is there any other way to process a large string?
  • Yes, as you are on the client side, you can use the shared memory techniques. You basically map a .net byte[] to an ArrayBuffer. See this (disclaimer: My library) or this library for reference on how to do it. These examples are using the binary content of actual javascript Files but it's applicable to strings as well. There is no reference documentation on these API's yet. Mostly just examples and the blazor source code.

  • Is there any other approach for processing large files?
  • See 2)

    Thanks @Tewr. I came across your library the first time I faced this problem. However, I couldn't get it to work first in under 5 mins and I stuck with my poor-man's version. Thanks a lot for your response. I'm giving you the correct answer :-) – Rodolfo Grave Oct 13, 2019 at 19:06 Thanks @Tewr, I tried to modify your source code for my purpose. It turns out byte[] can't be communicated via JSON protocol now. github.com/dotnet/aspnetcore/issues/11244 – code4j Jan 15, 2020 at 20:33 It should emphasized that this answer only applies to client-side scenarios, there is no such thing as shared memory nor an equivalent for server-side blazor. – Tewr Jan 17, 2020 at 12:23 @code4j, to pass byte[] over JSON (which I assume would be a server-side blazor scenario), encode the bytes it to a base64 string. plenty of examples out there on this subject. – Tewr Jan 23, 2020 at 20:29

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.