Skip to content
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

Provide better diagnostics for JS interop #24053

Closed
chucker opened this issue Jul 17, 2020 · 1 comment
Closed

Provide better diagnostics for JS interop #24053

chucker opened this issue Jul 17, 2020 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@chucker
Copy link

chucker commented Jul 17, 2020

Is your feature request related to a problem? Please describe.

I am trying to wrap a JS library (in this case, Leaflet), but the tooling makes mistakes hard and time-consuming to figure out.

For example, consider this JS code:

getSize: function (mapId) {
    return maps[mapId].getSize();
},

And then this C# wrapper around it:

public static async ValueTask<SizeF> GetSize(IJSRuntime jsRuntime, string mapId)
    => jsRuntime.InvokeAsync<SizeF>($"{_BaseObjectContainer}.getSize", mapId);

Similar methods exist, and work fine, but this one will return (when awaited) a SizeF of 0, 0. (Can you guess why? I couldn't — I assumed something was initialized wrong on the JS end.)

I had to add various logging code manually to figure out the issue: Leaflet's getSize() returns a Point, so its properties, from the C# side, would become X and Y, not Width and Height.

Trouble is, Blazor doesn't throw an exception, or log a warning — it just maps nothing, and thus the SizeF has an empty width.

Describe the solution you'd like

The methods on IJSInterop should have logging:

  1. there should be Debug (perhaps Trace) logging to always log (even in the presumed success case) how the C# in arguments get serialized to JSON, how the JS result comes back as JSON, and how that gets mapped back to C#
  2. there should be Warn logging to log when a mapping from JS back to C# didn't succeed. Due to lack of static typing, the reverse can't be validated, but this direction can: JSInterop knows which properties the generic type TValue has, and it knows if the returned JSON contains a property that the type does not.
  3. there should especially be a Warn logging if no properties were mapped even though a JSON object came back. That's rarely got to be the intended result.

Additional context

1 of those would've helped me track down dotnet/runtime#39208 a lot faster.
2/3 would have helped me today.

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Jul 17, 2020
@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
We rely on the System.Text.Json serializer for this, which currently doesn't support logging. Feel free to file a separate issue on the dotnet/runtime repo to add that support. When done, we can enable it on our side too.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants