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

Razor component disconnects Blazor when passing a class instance #19605

Closed
mrlife opened this issue Mar 5, 2020 · 6 comments
Closed

Razor component disconnects Blazor when passing a class instance #19605

mrlife opened this issue Mar 5, 2020 · 6 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Milestone

Comments

@mrlife
Copy link
Contributor

mrlife commented Mar 5, 2020

When visiting Index.cshtml, the browser console displays the below errors.

Index.cshtml

@{
	var myObject = new MyClass(1, "test");
}

<component type="typeof(MyComponent)" render-mode="ServerPrerendered" param-MyObject="@myObject" />

MyClass.cs

public class MyClass
{
	public int MyInt { get; set; }
	public string MyString { get; set; }

	public MyClass(int myInt, string myString)
	{
		MyInt = myInt;
		MyString = myString;
	}
}

MyComponent.razor

<p>
	@MyObject.MyInt
	@MyObject.MyString
</p>

@code {
	[Parameter]
	public MyClass MyObject { get; set; }
}

Browser console

Error: The list of component records is not valid.
Information: Connection disconnected.
Unhandled Promise Rejection: Error: Invocation canceled due to the underlying connection being closed.

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Further technical details

  • ASP.NET Core version
  • Include the output of dotnet --info
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
@guardrex
Copy link
Contributor

guardrex commented Mar 5, 2020

Related doc issue 👉 dotnet/AspNetCore.Docs#17210

... but I don't repro any such error here.

@pranavkm
Copy link
Contributor

pranavkm commented Mar 5, 2020

@mrlife parameters passed to the component need to be serializable by System.Text.Json. System.Text.Json does not support immutable types - https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#deserialize-to-immutable-classes-and-structs. You'll need to have a public parameterless constructor for this to work correctly.

@javiercn the exception should be thrown here: https://github.com/dotnet/aspnetcore/blob/master/src/Components/Server/src/Circuits/ComponentParameterDeserializer.cs#L68, but the exception is logged as Debug. Should this be surfaced more readily to the user?

@mrlife
Copy link
Contributor Author

mrlife commented Mar 5, 2020

@pranavkm Thanks for letting me know. I just tried it out, and it seems to work. Is this right?

Code

var myObject = new MyClass(1, "test");
var serialized = System.Text.Json.JsonSerializer.Serialize(myObject);

Contents of serialized

"{"MyInt":1,"MyString":"test"}"

@pranavkm
Copy link
Contributor

pranavkm commented Mar 5, 2020

It's the deserialization routine that needs a parameterless constructor.

@pranavkm pranavkm added the area-blazor Includes: Blazor, Razor Components label Mar 5, 2020
@mrlife
Copy link
Contributor Author

mrlife commented Mar 5, 2020

It's the deserialization routine that needs a parameterless constructor.

This is true, thank you. It works when serializing but does throw a runtime error when deserializing.

NotSupportedException: Deserialization of reference types without parameterless constructor is not supported.

@mkArtakMSFT mkArtakMSFT added the Docs This issue tracks updating documentation label Mar 5, 2020
@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Mar 5, 2020
@pranavkm pranavkm added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Mar 5, 2020
@ghost ghost added the Status: Resolved label Mar 5, 2020
@ghost
Copy link

ghost commented Mar 24, 2020

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Mar 24, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Apr 23, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants