Skip to content

Returning an HTML element from a JS interop call throws an exception. #23369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MackinnonBuck opened this issue Jun 26, 2020 · 8 comments
Open
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete feature-blazor-jsinterop This issue is related to JSInterop in Blazor Pillar: Technical Debt severity-minor This label is used by an internal tool
Milestone

Comments

@MackinnonBuck
Copy link
Member

Describe the bug

When using IJSRuntime.InvokeAsync<ElementReference>(...) to invoke a JS function that returns an HTML element, the following exception is thrown:

Unhandled exception rendering component: An exception occurred executing JS interop: __internalId is required..

This exception happens regardless of whether the HTML element being returned from the JS function is being passed in from .NET as an ElementReference.

To Reproduce

See https://github.com/MackinnonBuck/blazor-element-reference-bug. Build and run the Blazor project, then click the button labeled "Return ElementReference from JS". The full exception is displayed in the browser's console.

Exceptions

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: An exception occurred executing JS interop: __internalId is required.. See InnerException for more details.
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: __internalId is required.. See InnerException for more details. ---> System.Text.Json.JsonException: __internalId is required.
  at Microsoft.AspNetCore.Components.ElementReferenceJsonConverter.Read (System.Text.Json.Utf8JsonReader& reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) <0x2ce5ef0 + 0x000de> in <filename unknown>:0 
  at System.Text.Json.JsonPropertyInfoNotNullable`4[TClass,TDeclaredProperty,TRuntimeProperty,TConverter].OnRead (System.Text.Json.ReadStack& state, System.Text.Json.Utf8JsonReader& reader) <0x2ce5d50 + 0x0003c> in <filename unknown>:0 
  at System.Text.Json.JsonPropertyInfo.Read (System.Text.Json.JsonTokenType tokenType, System.Text.Json.ReadStack& state, System.Text.Json.Utf8JsonReader& reader) <0x2c6fea0 + 0x000c8> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.HandleValue (System.Text.Json.JsonTokenType tokenType, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& state) <0x2c6fc00 + 0x000a6> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.HandleObjectAsValue (System.Text.Json.JsonTokenType tokenType, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack, System.Text.Json.JsonReaderState& initialState, System.Int64 initialBytesConsumed) <0x2ce5ad0 + 0x000b6> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x2c796a8 + 0x00340> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x2c75cb0 + 0x00584> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x2c419c0 + 0x0003e> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.Deserialize (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x2c41858 + 0x00024> in <filename unknown>:0 
  at Microsoft.JSInterop.JSRuntime.EndInvokeJS (System.Int64 taskId, System.Boolean succeeded, System.Text.Json.Utf8JsonReader& jsonReader) <0x2cd9d40 + 0x00052> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x2cf2f20 + 0x0003c> in <filename unknown>:0 
  at Demo.App.ButtonClicked () [0x00046] in C:\blazor-element-reference-bug\Demo\App.razor:11 
  at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x2ccd598 + 0x000da> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2cd8278 + 0x000b6> in <filename unknown>:0 
f.printErr @ blazor.webassembly.js:1
f.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1
_mono_wasm_invoke_js_unmarshalled @ dotnet.3.2.0.js:1
do_icall @ dotnet.wasm:1
do_icall_wrapper @ dotnet.wasm:1
interp_exec_method @ dotnet.wasm:1
interp_runtime_invoke @ dotnet.wasm:1
mono_jit_runtime_invoke @ dotnet.wasm:1
do_runtime_invoke @ dotnet.wasm:1
mono_runtime_try_invoke @ dotnet.wasm:1
mono_runtime_invoke @ dotnet.wasm:1
mono_wasm_invoke_method @ dotnet.wasm:1
Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1
call_method @ dotnet.3.2.0.js:1
(anonymous) @ dotnet.3.2.0.js:1
endInvokeJSFromDotNet @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
Promise.then (async)
beginInvokeJSFromDotNet @ blazor.webassembly.js:1
_mono_wasm_invoke_js_marshalled @ dotnet.3.2.0.js:1
do_icall @ dotnet.wasm:1
do_icall_wrapper @ dotnet.wasm:1
interp_exec_method @ dotnet.wasm:1
interp_runtime_invoke @ dotnet.wasm:1
mono_jit_runtime_invoke @ dotnet.wasm:1
do_runtime_invoke @ dotnet.wasm:1
mono_runtime_invoke_checked @ dotnet.wasm:1
mono_runtime_try_invoke_array @ dotnet.wasm:1
ves_icall_InternalInvoke @ dotnet.wasm:1
ves_icall_InternalInvoke_raw @ dotnet.wasm:1
do_icall @ dotnet.wasm:1
do_icall_wrapper @ dotnet.wasm:1
interp_exec_method @ dotnet.wasm:1
interp_runtime_invoke @ dotnet.wasm:1
mono_jit_runtime_invoke @ dotnet.wasm:1
do_runtime_invoke @ dotnet.wasm:1
mono_runtime_try_invoke @ dotnet.wasm:1
mono_runtime_invoke @ dotnet.wasm:1
mono_wasm_invoke_method @ dotnet.wasm:1
Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1
call_method @ dotnet.3.2.0.js:1
(anonymous) @ dotnet.3.2.0.js:1
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
s @ blazor.webassembly.js:1
e.invokeMethodAsync @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
t.dispatchEvent @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
e.onGlobalEvent @ blazor.webassembly.js:1

Further technical details

Blazor WebAssembly project (.NET standard 2.1)

Output of dotnet --info:

.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.6.20318.15
 Commit:    4356580024

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\

Host (useful for support):
  Version: 5.0.0-preview.6.20305.6
  Commit:  4ba9ecaabd

.NET SDKs installed:
  3.1.301 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.6.20318.15 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.6.20312.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.6.20305.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.6.20308.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

IDE: Visual Studio Enterprise 2019

@MackinnonBuck MackinnonBuck added bug This issue describes a behavior which is not expected - a bug. area-blazor Includes: Blazor, Razor Components labels Jun 26, 2020
@marinasundstrom
Copy link

Is there a workaround?

@SteveSandersonMS SteveSandersonMS added affected-most This issue impacts most of the customers severity-minor This label is used by an internal tool labels Oct 7, 2020 — with ASP.NET Core Issue Ranking
@marinasundstrom
Copy link

Related: #26496

@marinasundstrom
Copy link

marinasundstrom commented Dec 18, 2020

Just to make it clear: As I get it: ElementReference is a platform-independent Blazor concept, not a JS concept. So you should use IJSObjectReference when passing JavaScript references that do not map to what Blazor keeps track of as part of its render tree.

Issues arise when using older libraries that are using ElementReference but not IJSObjectReference which is quite new.

Community-provided BlazorCanvas, for example. Images to be drawn required to be passed as ElementReference which does not allow for dynamic loading in JavaScript - since you cannot and should not return an ElementReference from JavaScript. The reason is that it is not being tracked by Blazor and does not have an id,

However, you can pass an ElementReferenceto JavaScript since it then represents an actual element rendered by Blazor.

@javiercn javiercn added the feature-blazor-jsinterop This issue is related to JSInterop in Blazor label Apr 20, 2021
@TanayParikh
Copy link
Contributor

Related to #26049?

@TanayParikh TanayParikh modified the milestones: Backlog, .NET 7 Planning Oct 19, 2021
@TanayParikh TanayParikh added affected-few This issue impacts only small number of customers and removed affected-most This issue impacts most of the customers labels Oct 21, 2021
@TanayParikh TanayParikh modified the milestones: .NET 7 Planning, Backlog Oct 21, 2021
@ghost
Copy link

ghost commented Oct 21, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@TanayParikh TanayParikh added the cost: S Will take up to 2 days to complete label Jun 24, 2022
@mkArtakMSFT mkArtakMSFT modified the milestones: Backlog, .NET 8 Planning Oct 21, 2022
@ghost
Copy link

ghost commented Oct 21, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Oct 6, 2023

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Nov 6, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete feature-blazor-jsinterop This issue is related to JSInterop in Blazor Pillar: Technical Debt severity-minor This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests

7 participants