Skip to content

Dereference the result of createJSObjectReference from JavaScript #56200

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
Yannike opened this issue Jun 12, 2024 · 12 comments
Closed

Dereference the result of createJSObjectReference from JavaScript #56200

Yannike opened this issue Jun 12, 2024 · 12 comments
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue Status: No Recent Activity

Comments

@Yannike
Copy link

Yannike commented Jun 12, 2024

          What about how to dereference the result of  createJSObjectReference from JavaScript? I have a JavaScript object that is not Json serializable but I want to keep a reference to it on the blazor side and call function on it from the JavaScript side.

Originally posted by @Yannike in #53161 (comment)

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jun 12, 2024
@Yannike
Copy link
Author

Yannike commented Jun 12, 2024

Sorry was just looking at the original post (linked in the issue description) and noticed it had been prematurely closed with multiple comments still unanswered

@javiercn
Copy link
Member

javiercn commented Jun 12, 2024

@Yannike what do you exactly mean by dereferencing?

Do you mean accessing a property of the JSObjectReference directly?

You might be looking for #31151 which is not currently supported. You need to add a method to your object so that it can be called that way

If you give us an example in code that might help us understand what you are trying to accomplish.

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jun 12, 2024
@Yannike
Copy link
Author

Yannike commented Jun 14, 2024

So here is my situation. I have a javascript object that is not Json serializable. I want to be able to keep a reference to the object on the c# side and send it back to Javascript to call on method on the object.

for example the object could be created on the js (typescript) side with this :

static async CreateObject(): Promise<any> {
	let newObject = new JavascriptObject();
	return DotNet.createJSObjectReference(newObject);
}

on the blazor side

IJSObjectReference jsObjectReference = await this.jsRuntime.InvokeAsync<IJSObjectReference>("app.CreateObject");
jsObjectReference.InvokeVoidAsync("test", this.objectReference)

I want to call a test method that take the object reference as a parameter and that is able to use this object in javascript. So far I haven't manage to make it work.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jun 14, 2024
@javiercn
Copy link
Member

javiercn commented Jun 14, 2024

@Yannike thanks for the additional details.

What is this.objectReference in your example?

If its a dotnet object that you are passing down to JS, you need to use JSRuntime.CreateDotNetObjectReference(<<your-object>>) to get a reference that can be passed down.

@javiercn javiercn added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 14, 2024
@Yannike
Copy link
Author

Yannike commented Jun 14, 2024

Sorry Javier, the this.objectReference should be jsObjectReference, so the reference returned from JavaScript

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jun 14, 2024
@javiercn
Copy link
Member

@Yannike thanks for the additional details.

So, to make sure that we understand what you are trying to do, will you say that the following sequence accurately represents what you are trying to do?

sequenceDiagram
    Dotnet ->> JS: JSObjectReference reference = await JSRuntime.InvokeAsync("GetJSObjectReference")
    JS -->> Dotnet: return [JSObjectReference]
    Dotnet ->> JS: var result = reference.Invoke("Test", reference)
    break
      Dotnet --> JS: JS Interop error
    end
Loading

@javiercn javiercn added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 14, 2024
@Yannike
Copy link
Author

Yannike commented Jun 17, 2024

Yes that is exactly what I am trying to do. Nicely described !

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jun 17, 2024
@javiercn
Copy link
Member

javiercn commented Jun 17, 2024

@Yannike thanks for the additional details.

From what I can tell looking at the code, this should work. Can you provide a minimal repro project as a public github repository that demonstrates the issue? There might be small details that are different that we aren't mentioning explicitly here.

@javiercn javiercn added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 17, 2024
Copy link
Contributor

Hi @Yannike. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@javiercn javiercn added the Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue label Jun 17, 2024
Copy link
Contributor

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.

Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue Status: No Recent Activity
Projects
None yet
Development

No branches or pull requests

2 participants