question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor.platform.readStringField not working anymore after dotnet 7.0 upgrade

See original GitHub issue

Description

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: image

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: image

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:open
  • Created 10 months ago
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
nkastcommented, Jan 20, 2023

@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.

1reaction
nkastcommented, Nov 24, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

7.0.x Milestone
Blazor.platform.readStringField not working anymore after dotnet 7.0 upgrade arch-wasm area-blazor Includes: Blazor, Razor Components feature-blazor-wasm ...
Read more >
Getting 'import.meta' error when updating blazor app to ...
According to this issue the problem is having some outdated packages as stated by @Mister Magoo. Updating WebAssembly did the trick for me....
Read more >
NET 7.0 Framework option is not available after installation ...
I downloaded the .NET 7.0 from here. And installed it on my Windows machine but while creating the project, I am not able...
Read more >
ASP.NET Core updates in .NET 7 Preview 7
NET 7 Preview 7 is now available and includes many great new improvements to ASP.NET Core. ... Blazor data binding get/set/after modifiers.
Read more >
ASP.NET Core updates in .NET 8 Preview 4
As the async tasks complete, the updated content is streamed to the response and then patched by Blazor into the DOM.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found