dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.43k stars 10.02k forks source link

.NET 8 Blazor - Provide Better Developer Feedback for Parameter Deserialization Issues #54002

Open sbwalker opened 8 months ago

sbwalker commented 8 months ago

Is there an existing issue for this?

Describe the bug

If you are transitioning from Static rendering to Interactive rendering and you are passing parameters from one component to another, it is critical that the parameters are serializable. If a parameter is not serializable you will get a cryptic run-time error in the browser console which does not provide any indication of the root cause:

DeserializationRepro.razor

<InteractiveComponentWithParameter MissingConstructor="@_missingConstructor" @rendermode="InteractiveServer" />

@code {
    private MissingConstructor _missingConstructor = new MissingConstructor("https://www.microsoft.com/");
}

InteractiveComponentWithParameter.razor

@code {
    [Parameter]
    public MissingConstructor MissingConstructor { get; set; }
}

MissingConstructor.cs

    public class MissingConstructor
    {
        //public MissingConstructor() { }

        public MissingConstructor(string url)
        {
            Uri uri = new Uri(url);
            Host = uri.Host;
        }

        public string Host { get; set; }
    }

Running this code will result in a browser console error:

"Error: The list of component operations is not valid."

image

Note that to resolve this issue you simply need to uncomment the parameterless constructor in the MissingConstructor.cs class

    public class MissingConstructor
    {
        public MissingConstructor() { }

        public MissingConstructor(string url)
        {
            Uri uri = new Uri(url);
            Host = uri.Host;
        }

        public string Host { get; set; }
    }

Expected Behavior

Provide an error message which is meaningful and helps a developer identify the source of the problem:

ie. "An error occurred deserializing a component parameter"

Even better would be any reference to the actual component name or parameter name causing the issue.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.1

Anything else?

No response

clarity99 commented 8 months ago

This! It took me most of the day to figure out that serialization is the issue, when my component wasn't working when I set the parameter to IQueryable data source.

T-mp commented 5 months ago

Is there currently a known way to determine the affected class/parameter? Debugging is not possible because the error only occurs on the server, not when I start it from Visual Studio.

hunterlan commented 2 weeks ago

This! It took me most of the day to figure out that serialization is the issue, when my component wasn't working when I set the parameter to IQueryable data source.

I spent several days to understand that I'm facing same problem when I use IEnumerable data source. What I don't understand is why with these collections we have deserialization issues, if according to this article, mentioned collection types are easily (de)serializable. Anyway, as a workaround, I just wrap collections to class and send this class to component.

UPD: Also in my case, when required requirement was not met, it also calls to log an error in client console.