kofin
kofin

Reputation: 47

Sending custom class via SignalR to Blazor WebAssembly

I have issue with sending custom class via SignalR hub to client (Blazor WebAssembly). I have worker process, that is periodically sending data to all connected clients. If I try to send standard data, string, List of strings, Date etc. Client can process it without any problems. But when I try to send my own "data class" I am receiving this error

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'

Here is my custom class

Custom 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;
    }
}

How server is sending data from Worker

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

How client is reading data

    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();
    });

Upvotes: 0

Views: 590

Answers (1)

Noah Stahl
Noah Stahl

Reputation: 7613

Try adding a constructor to your Project class without parameters, for example:

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

Upvotes: 2

Related Questions