Search code examples
asp.net-coresignalrblazorise

How to use signalR in blazorise


I'm using SignalR in asp.net core on server side and blazorise on client side. before I've used SignlaR basically just to chat and now I wanna use it to update a table when a record is inserted in some where else. I think everything is ok on server side because as I trace it on server, it posts correct values but it does not update the table on client side. I don't know what's wrong. here is my code on server side which is in a hub:

public async Task SendCartableUpdate(ResultData<PersonnelStationsInfo> resultData)
    {
        await Clients.All.SendAsync("RefreshCartable",resultData);
    }

and this is how I use it on client side:

protected override async Task OnInitializedAsync()
    {
        //await base.OnInitializedAsync();
        user = CurrentUserService.CurrentUser;
        await CartableTableChangePage(1);

        hubConnection = new HubConnectionBuilder()
    .WithUrl(navigationManager.ToAbsoluteUri("http://localhost:15424/ProductionServiceHub"))
    .Build();
        hubConnection.On<ResultData<PersonnelStationsInfo>>("RefreshCartable", (_resultData) =>
        {
            StateHasChanged();
        });
        await hubConnection.StartAsync();
    }

thanks for your helping


Solution

  • Finally I solved it and I'm so excited :) here is how I changed my code on server side:

    public async Task SendCartableUpdate()
        {
            await Clients.All.SendAsync("RefreshCartable");
        }
    

    and this how I changed my code on client side:

     protected override async Task OnInitializedAsync()
    {
        //await base.OnInitializedAsync();
        hubConnection = new HubConnectionBuilder()
    .WithUrl(navigationManager.ToAbsoluteUri("http://192.168.2.72:1050/ProductionServiceHub"))
    .Build();
        hubConnection.On("RefreshCartable", () =>
        {
            CallLoadData();
            StateHasChanged();
        });
        await hubConnection.StartAsync();
    
        user = CurrentUserService.CurrentUser;
        await CartableTableChangePage(1);
    }
    
    private void CallLoadData()
    {
        Task.Run(async () =>
        {
            await CartableTableChangePage(1);
            StateHasChanged();
        });
    }