Skip to content

Custom event args cannot include byte arrays or DotNetObject instances #34338

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

Closed
SteveSandersonMS opened this issue Jul 14, 2021 · 2 comments · Fixed by #35038
Closed

Custom event args cannot include byte arrays or DotNetObject instances #34338

SteveSandersonMS opened this issue Jul 14, 2021 · 2 comments · Fixed by #35038
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Milestone

Comments

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Jul 14, 2021

This is a small bug introduced recently (by me, I think) due to an optimization. Update: No, after investigation, I realise this was never supported, because the event dispatch was not via JS interop (on Server and WebView).

Our updated logic for dispatching events now does its own JSON serialization (so it can do its own UTF8 encoding, so the .NET side doesn't have to convert UTF8->String->UTF8). The logic looks like const utf8 = textEncoder.encode(JSON.stringify(data));

This is inconsistent with how the serialization works for JS interop, in that JS interop uses its own arg replacer that deals with things like formatting DotNetObject or Uint8Array.

We do actually have an E2E test showing you can include special types in the event payload, but unluckily, it happens to be IJSObjectReference that happens to serialize into the right shape using JSON.stringify alone.

Fixing this

... should be very simple. We export some new function stringifyAsArgs or similar onto JS interop's DotNet, and call that instead of JSON.stringify.

Better still, we should bake in this "I want to send it as UTF8 bytes" pattern, since we're using it in multiple places. We could export a special type Utf8BytesParameter from DotNet that changes the serialization rule for that parameter.

Update: No, those suggestions are nonsense. The second one doesn't make sense because we're not sending the message via JS interop, so there's nothing to implement this. The first one is bad because it breaks the layering and means we wouldn't be able to guarantee that things like byte[] messages correctly appear just before corresponding JS interop calls. A bigger solution is needed.

@SteveSandersonMS SteveSandersonMS added the area-blazor Includes: Blazor, Razor Components label Jul 14, 2021
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Jul 15, 2021
@mkArtakMSFT mkArtakMSFT added this to the 6.0-rc1 milestone Jul 15, 2021
@SteveSandersonMS
Copy link
Member Author

Make sure the fix also covers #34339 (comment)

@SteveSandersonMS
Copy link
Member Author

On further investigation, this does not actually appear to be a regression. It looks as if we've never supported byte arrays or DotNetObject instances in custom event args data, since we've always called JSON.stringify inside boot.*.ts on the event args object.

@ghost ghost added the Working label Aug 4, 2021
@SteveSandersonMS SteveSandersonMS changed the title Custom events can no longer include byte arrays or DotNetObject instances Custom event args cannot include byte arrays or DotNetObject instances Aug 4, 2021
@ghost ghost added Done This issue has been fixed and removed Working labels Aug 5, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants