Circular references are not supported, take two
See original GitHub issueAs correctly mentioned in #18628, blazor currently doesn’t support circular references to be serialized. But I’m very confused as to what happened to that issue. It’s clear from the sources that the current implementation uses JSON.stringify
before actually invoking the C# code and that function throws a TypeError: cyclic object value
when you pass it an object that contains circular references.
#18628 was closed because this is covered in an external issue (https://github.com/dotnet/runtime/issues/30820) but I don’t understand how that’s the case. It’s true that the serializer can’t handle circular references yet but once that issue is fixed and the serializer does have support for that, the error in blazor will persist. That’s because the error already happens at JSON.stringify
which isn’t affected by blazor.
I debugged it manually by building and including the source from Microsoft.JSInterop.JS.
When you step through the code you can see that the error happens at JSON.stringify
(in my case at Line 61 / invokePossibleInstanceMethod
). This is the case when synchronously invoking C# code from JavaScript with an argument that contains circular references. The error that happens is as follows (in Firefox):
TypeError: cyclic object value
When you do it the other way around (invoke JavaScript from C# with a return value that contains circular references), the error will be rethrown in C# and the error changes to the following. When you debug it you can observe that it’s also thrown at a JSON.stringify
call.
WASM: Microsoft.JSInterop.JSException: cyclic object value
From my understanding, the error would be exactly the same if the serializer was able to handle circular references because the exception is thrown in JavaScript at JSON.stringify
and doesn’t ever reach the .NET serializer.
Am I completely missing the point here or are my assumptions correct? I would’ve commented on the other issue but it’s locked.
If I’m correct, will this be supported once https://github.com/dotnet/runtime/issues/30820 is implemented? I’m in need of a solution right now so I’ll have to take a look at flatted or something similar in the meantime.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
I have the exact same problem as @Joelius, with the same use case of trying to wrap a JS library in Blazor Webassembly. I need to return complex JS objects to .Net, that include references, some of them circular. I would like the ability to: 1. Detect and ignore the circular references; 2. Set a maximum depth of serialization.
@gb1337 The depth of serialization issue might get fixed with #12685. Make sure to explain your use-case there, it’ll increase the chances of it being implemented soon.