Blazor 预览版 9/mono-wasm 内存访问越界:DotNet.invokeMethod 的最大字符串大小?

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

自 dotnet core 3 预览版 9 以来,我在调用 dotnet 方法时遇到了一个问题,该方法从 JavaScript.

传递一个大字符串

代码超过一千字,因此下面的代码片段重现了这个问题。它在 length = 1 * mb 时有效,但在 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) { }
}

错误信息是:

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)

我需要处理代表文件内容的大字符串。

  1. 有没有办法提高这个限制?
  2. 除了将字符串分解成多个段并执行多次调用之外,还有其他方法可以处理大字符串吗?
  3. 还有其他处理大文件的方法吗?

这曾经在预览版 8 中有效。

  1. Is there a way to increase this limit?

否(除非您修改并重新编译 blazor mono/wasm)。

  1. Apart from breaking down the string into multiple segments and performing multiples calls, is there any other way to process a large string?

是的,因为你在客户端,你可以使用共享内存技术。您基本上将 .net byte[] 映射到 ArrayBuffer。参见 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

  1. Is there any other approach for processing large files?

参见 2)

我在 netcore 3.2 Blazor 应用程序中重现了您的问题(正如您所描述的那样,1 到 2 Mb 之间的数据会杀死它)。我将应用程序更新到 netcore 5.0 并且问题得到解决(当我向它扔 50Mb 时它仍在工作)。