通过 SignalR 将自定义 class 发送到 Blazor WebAssembly

Sending custom class via SignalR to Blazor WebAssembly

我在通过 SignalR hub 向客户端 (Blazor WebAssembly) 发送自定义 class 时遇到问题。 我有工作进程,它会定期向所有连接的客户端发送数据。如果我尝试发送标准数据、字符串、字符串列表、日期等。客户端可以毫无问题地处理它。 但是当我尝试发送自己的“数据 class”时,我收到了这个错误

Microsoft.AspNetCore.SignalR.Client.HubConnection[57] Failed to bind arguments received in invocation '(null)' of 'ReceiveProject'. System.IO.InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked. ---> System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'SekvenceReport.Shared.Project'

这是我的习惯class

自定义class

public class Project
{
    public Project(string name, string[] dbtable)
    {
        this.Name = name;
        this.DbTable = dbtable;
        this.Slbs = new List<Slb>();
        this.Duplicity = true;
        this.EdiError = true;
    }
    public string Name { get; set; }
    public string[] DbTable { get; set; }

    public List<Slb> Slbs { get; set; }

    public bool Duplicity { get; set; }
    public bool EdiError { get; set; }
}

public class Slb
{
    public Slb(string slbid)
    {
        this.SlbId = slbid;
        this.Sekvences = new List<Sequence>();
        this.Status = "Připraveno";
    }
    public Slb()
    {
    }
    public string SlbId { get; set; }
    public List<Sequence> Sekvences { get; set; }
    public string Status { get; set; }
    public string StatusAsn { get; set; }
    public string StatusEvaluation()
    {
        string eval = this.Status;

        if (this.Sekvences.Any(i => i.Status == "D") && this.Sekvences.Any(i => i.Status == "N"))
        {
            eval = "Sekvencování";
            this.Status = eval;
            return this.Status;
        }
        else if (this.Sekvences.Any(i => i.Status == "D"))
        {
            eval = "Uvolněno";
            this.Status = eval;
            return this.Status;
        }
        else if (this.Sekvences.Any(i => i.Status == "N"))
        {
            if (this.Sekvences.Any(i => i.LoadingStatus == true) && this.Sekvences.Any(i => i.LoadingStatus == false))
            {
                eval = "Nakládání";
                this.Status = eval;
                return this.Status;
            }
            else if (this.Sekvences.Any(i => i.LoadingStatus == false))
            {
                eval = "Nasekvencováno";
                this.Status = eval;
                return this.Status;
            }
            else if (this.Sekvences.Any(i => i.LoadingStatus == true))
            {
                eval = "Naloženo";
                this.Status = eval;
                return this.Status;
            }
        }

        eval = "Neznámý stav";
        this.Status = eval;
        return eval;
    }
}

服务器如何从 Worker 发送数据

await _projectHubContext.Clients.All.SendAsync("ReceiveProject", Data.projects);

客户端如何读取数据

    hubConnection = new HubConnectionBuilder()
        .WithUrl(NavigationManager.ToAbsoluteUri("/projecthub"))
        .ConfigureLogging(logging => logging.AddProvider(LoggerProvider))
        .Build();


    //Processing custom class (List of Projects)
    hubConnection.On<List<Project>>("ReceiveProject", (listProjects) =>
    {
        var vars = listProjects;
        //projects.AddRange(listProjects);
        projects.AddRange(vars);
        StateHasChanged();
    });

尝试向您的项目添加构造函数 class without parameters,例如:

public class Project
{
    public Project()
    {
    }
    
    // Rest of class
}