Closed JinShil closed 1 year ago
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.
Same issue here when trying to send objects from JS to .Net in a callback situation. I would like to receive the objects as IJSObjectReference on the .Net side and not have JSON attempt to serialize them. Is there an inverse to attachReviver so that I may influence the serialization process from JS to .Net? Is it attachDispatcher??
This my device I'm the owner here so whoever that user is please deny access to my private repository list
jag
On Tue, Sep 14, 2021, 1:24 PM user72356 @.***> wrote:
Same issue here when trying to send objects from JS to .Net in a callback situation. I would like to receive the objects as IJSObjectReference on the .Net side and not have JSON attempt to serialize them. Is there an inverse to attachReviver so that I may influence the serialization process from JS to .Net? Is it attachDispatcher??
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dotnet/aspnetcore/issues/26049#issuecomment-919405240, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU3JA5SS3BSJENWHV4W6DCTUB6HO7ANCNFSM4RRWPYNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You've been hacked! Evil laughter
- What is an "Unexcepted JSON property"? Did you mean "Unexpected"?
Yes, this was a typo and has been fixed for 6.0
The progress on solving this problem is also kept track of in issue #31151
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.
This is a bug in the application code. The JavaScript code should look like this:
dotNetObj.invokeMethod(dotNetMethod, DotNet.createJSObjectReference(e));
That is, you must explicitly construct the JS object reference via DotNet.createJSObjectReference
for calls originating from JS, otherwise the system has no way to know that's how you want to serialize the parameter value.
@guardrex Do you know if this is covered in docs anywhere? I searched through https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-7.0 and the createJSObjectReference
API doesn't appear to be mentioned. This might be something we missed.
Yes, this was a typo and has been fixed for 6.0
@TanayParikh Was it definitely fixed? I was still able to repro this message on 7.0:
@SteveSandersonMS ... Correct ... not covered ... so I'll open an issue and hopefully reach it by the end of next week. I have about a week of .NET 7 wrap-up things to do first.
Was it definitely fixed? I was still able to repro this message on 7.0:
That was in reference to a correction I'd made in one of my streaming/byte[] interop PRs. But just took another look and there are a lot of places we say excepted
instead of expected
.
@SteveSandersonMS @javiercn ... In @JinShil's example (in the context of creating a doc example to demonstrate this), why doesn't the event marshal back to .NET as a MouseEventArgs
?
[JSInvokable]
public void OnClick(MouseEventArgs args)
{
Console.WriteLine($"Mouse coordinates: {args.ClientX}:{args.ClientY}");
}
... because I think that the click
event makes the Event
(e
) a MouseEvent
. When I read e.clientX
and e.clientY
, I do get mouse coordinates. However on the .NET side, I'm getting ...
Mouse coordinates: 0:0
No 💥 ... but no values.
If it turns out that it should marshal back as a IJSObjectReference
in this case (i.e., a click event on a <div>
), what would be good to do with objRef to complete the demo for this?
[JSInvokable]
public void OnClick(IJSObjectReference objRef)
{
// ??????????
}
In @JinShil's example (in the context of creating a doc example to demonstrate this), why doesn't the event marshal back to .NET as a MouseEventArgs?
Because it isn't one. It's a JavaScript MouseEvent
object, which not the same type as a .NET MouseEventArgs
(even though they are clearly related to the same concept). For real Blazor event handlers, Blazor contains internal code that explicitly marshals between the JS event types and the .NET event types, passing the specific subset of data that makes sense to serialize. But this logic isn't a real Blazor event handler; it's just a JS interop call, so Blazor will just do normal JSON serialization without any other special-case marshalling.
If it turns out that it should marshal back as a IJSObjectReference in this case (i.e., a click event on a
), what would be good to do with objRef to complete the demo for this?I wouldn't have an example based on event handlers, because Blazor has a proper native way to deal with events, and it's not this.
Instead I'd simply show an example of sending some arbitrary object from JS to .NET as an
IJSObjectReference
. For example,// Passes a reference to the non-serializable 'window' object to .NET code, which receives it as an IJSObjectReference DotNet.invokeMethodAsync('MyApp', 'ReceiveWindowObject', DotNet.createJSObjectReference(window));
Very well ... one final question about an API remark for createJSStreamReference
, which I'm also placing in the doc ...
@param streamReference The ArrayBufferView or Blob used to create the JavaScript stream reference.
export function createJSStreamReference(streamReference: ArrayBuffer | ArrayBufferView | Blob | any)
I can't cross-link "ArrayBufferView" at MDN (or in the ECMAScript spec either). I see it described around the Net as a "superclass" for accessing the bytes of ArrayBuffer
... or an "abstract type" for various arrays, but MDN doesn't have a dedicated page to link for it. I was angling the text to read something like this ...
Call
DotNet.createJSStreamReference(streamReference)
to construct a JS stream reference so that it can be passed to .NET, wherestreamReference
is anArrayBuffer
,ArrayBufferView
, orBlob
used to create the JS stream reference.
No example on that one. I was just going to place it under the new content for createJSObjectReference
in a section titled Create JavaScript object and data references that can be passed to .NET.
Is an "ArrayBufferView
" a DataView
of an ArrayBuffer
and/or a TypedArray
of an ArrayBuffer
?
Or ... should I just leave "ArrayBufferView" code-fenced without an MDN cross-link?
Is an "ArrayBufferView" a DataView of an ArrayBuffer and/or a TypedArray of an ArrayBuffer?
I think ArrayBufferView
is just the abstract base class for all the different kinds of typed array. So I'd probably say something like:
Call DotNet.createJSStreamReference(streamReference) to construct a JS stream reference so that it can be passed to .NET, where streamReference is an ArrayBuffer, Blob, or any typed array such as Uint8Array or Float32Array used to create the JS stream reference.
TBH the parameter name streamReference
is really strange. It should probably be called dataSource
or something like that.
Ok, I'm good. I'll wrap this up now. If you want me to ping u on the PR, let me know, but I don't think it's necessary based on how closely the text will come off of your remarks (and the example).
Thanks. Closing this issue now then.
Trying to return a DOM Event object as an
IJSObjectReference
throws an exception:IJSObjectReference
to theJSInvokable
OnClick
function below.To Reproduce
Bug.csproj
Javascript to add to index.html
Index.razor
Further technical details