Blazor.platform.readStringField not working anymore after dotnet 7.0 upgrade
See original GitHub issueDescription
Hello, I’m currently a user of nKast’s WASM implementation of MonoGame: https://github.com/nkast/Wasm https://github.com/kniEngine/kni
However after a dotnet 7 upgrade this logic doesn’t work anymore.
I’ve been trying to dive into the issue here but it seems the code that was previously used to pass parameters to the frontend isn’t working anymore:

Previously the var id should have the value theCanvas.
The way this functionality is being called can be found here: https://github.com/nkast/Wasm/blob/main/Wasm.Dom/Document.cs#L40
Reproduction Steps
I created a branch of my 3d Maze Generator making use of MonoGame / Wasm to run in the browser here: https://github.com/devedse/DeveMazeGeneratorCore/tree/dotnet7
Simply open the DeveMazeGeneratorCoreMonoGame.sln, set the Blazor project as startup and click F5.
This error should popup in the console window:
Unhandled Exception:
blazor.webassembly.js:1 System.NullReferenceException: Object reference not set to an instance of an object.
dotnet.7.0.0.3dkjxfc2jf.js:5 Uncaught Error: System.NullReferenceException: Object reference not set to an instance of an object.
at :5259/MonoGame.Framework.BlazorGameWindow.get_ClientBounds() in C:\XGit\kni\MonoGame.Framework\Platform\Blazor\BlazorGameWindow.cs:line 62
at Microsoft.Xna.Platform.ConcreteGraphicsDeviceManager..ctor(:5259/Game game) in C:\XGit\kni\MonoGame.Framework\ConcreteGraphicsDeviceManager.Blazor.cs:line 16
at Microsoft.Xna.Framework.GraphicsDeviceManager..ctor(:5259/Game game) in C:\XGit\kni\MonoGame.Framework\GraphicsDeviceManager.cs:line 39
at DeveMazeGeneratorMonoGame.TheGame..ctor(:5259/IContentManagerExtension contentManagerExtension, Nullable`1 desiredScreenSize, Platform platform) in C:\XGitPrivate\DeveMazeGeneratorCore\DeveMazeGeneratorCore.MonoGame.Core\TheGame.cs:line 152
at :5259/DeveMazeGeneratorCore.MonoGame.Blazor.Pages.Index.TickDotNet() in C:\XGitPrivate\DeveMazeGeneratorCore\DeveMazeGeneratorCore.MonoGame.Blazor\Pages\Index.razor.cs:line 37
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(:5259/JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson)
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(:5259/JSRuntime jsRuntime, DotNetInvocationInfo& invocationInfo, String argsJson)
at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.InvokeDotNet(:5259/String assemblyName, String methodIdentifier, String dotNetObjectId, String argsJson)
at qi (dotnet.7.0.0.3dkjxfc2jf.js:5:80156)
at Ji (dotnet.7.0.0.3dkjxfc2jf.js:5:80040)
at _Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet (_Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet:30:5)
at Object.invokeDotNetFromJS (blazor.webassembly.js:1:45192)
at g (blazor.webassembly.js:1:1621)
at A.invokeMethod (blazor.webassembly.js:1:3812)
at tickJS ((index):61:32)
Expected behavior
I know that this behaviour is now deprecated (as of: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/import-export-interop?view=aspnetcore-7.0#call-javascript-from-net)
However I would expect the funcionality to remain working.
Actual behavior
The Blazor.platform.readStringField is returning:

but should return theCanvas
Regression?
No response
Known Workarounds
Probably rewrite everything to [JSImport] [JSExport], but that’s quite some work I’d rather not do now.
Configuration
dotnet 7.0 X64 Google Chrome
Other information
No response
Issue Analytics
- State:
- Created 10 months ago
- Comments:22 (9 by maintainers)

Top Related StackOverflow Question
@kg The workaround worked. I had to add an additional element to the ValueTuple as padding. workaround fix for issue dotnet/runtime#78731
GetElementById is passed in a ValueTuple, which effectively is a struct.
I found another example that is very similar. https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-5.0#unmarshalled-javascript-interop-2 It uses a struct (‘InteropStruct’) to pass values to JS and the readStringField/readInt32Field methods. In my test I am passing two integer values before and after the string which I can read successfully with readInt32Field from offsets 0 and 8.