Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor: The list of component records is not valid

Tags:

blazor

using Blazor dotnet core 3.1 and getting this error:

The list of component records is not valid

Found a bug report regarding this that was closed in december 2019: https://github.com/dotnet/aspnetcore/issues/14966 enter image description here Don't see any reason for this error and hope there is a workaround (can not make this error each page call, it's just happening very rarely...).

Does anybody has an idea / hint which could cause this?

Thx!

like image 747
baer999 Avatar asked Feb 08 '20 08:02

baer999


4 Answers

I hit the same issue when I upgraded my app from .NET Core 3.0 to .NET Core 3.1 while using Blazor Client. The first "fix" I found was to do a force-refresh on the client (the web browser). That is, I pressed Ctrl+F5 to refresh.

I'm assuming that there was some API call that was being cached on the client and due to some version/format changes from 3.0 to 3.1, the old cached data was no longer valid, causing an error.

I'll be contacting the people who work on Blazor to try to get more info (I work with them at Microsoft).


Update June 1, 2020

I hit this again running my app on Azure App Services. This time even Ctrl+F5 didn't work. But I found https://stackoverflow.com/a/59356356/31668, and applied the fix there, which seemed to fix my problem.

I made a slight change to the code because in my case I use the Azure SignalR service only in staging/production and not in development. So I have this code in my app's Startup.cs ConfigureServices method:

if (!HostingEnvironment.IsDevelopment())
{
    services.AddSignalR().AddAzureSignalR(options =>
    {
        options.ServerStickyMode = Microsoft.Azure.SignalR.ServerStickyMode.Required;
    });
}
like image 119
Eilon Avatar answered Sep 25 '22 22:09

Eilon


This issue may happen by browser's automatic Cache control.

Let's say you have two MVC views with Blazor components within: Pages A.cshtml and B.cshtml.

When requesting page A, you receive a html response with the Blazor component rendered and a connection is stablished. Then, you navigate to page B. If you go back to page A (e.g. pressing back button) your browser will use a cached response made previously to fetch page A.

This should break the connection between Client and Server as the component descriptors may be changed. (sometimes seems randonmly).

To solve this, disable this cache behaviour on your _Host.cshtml adding:

@{ 
    Context.Response.Headers["Cache-Control"] = "no-store";
}

This will prevent the caching from your browser and make it request a new page with a correct components descriptor.

References: https://www.gitmemory.com/issue/dotnet/aspnetcore/26174/705472525 https://github.com/dotnet/aspnetcore/issues/18143#issuecomment-585961784

like image 39
Vagner Gon Avatar answered Sep 23 '22 22:09

Vagner Gon


In my case it was a class that I was using as a cascade value, but it didn't have a constructor without parameters

public class User 
{
            
    public User(ClaimsPrincipal principal) 
    {
       ....
    }
       ....
 }

 public class User 
 {
    //this fix
    public User(){}
    public User(ClaimsPrincipal principal) 
    {
       ....
    }
       ....
 }

[CascadingParameter]
private User User { get; set; }
like image 24
Marcio Goularte Avatar answered Sep 24 '22 22:09

Marcio Goularte


In my case problem "The list of component records is not valid" was when Blazor need to serialize parameter for component in _Host.cshtml

<component type="typeof(App)" render-mode="Server" param-SessionData="@Model.SessionData" />

After long searching of the issue I've found that problem was because of NameValueCollection that was cloning in SessionData before serialization. I dont why but when I excluded variable with NameValueCollection type issue was resolved.

SessionData is a class that has a Query property of type NameValueCollection and its values were cloned in a standard way:

Query = (Query?.Count ?? 0) != 0 ? new NameValueCollection(Query) : new NameValueCollection()

I've try clone with for loop function for each item in collection but it's not helped. Only after exclusion of Query property problem was resolved.

like image 40
Vitalii Vagin Avatar answered Sep 24 '22 22:09

Vitalii Vagin