Signalr netCore 5 Sendasync inside an On Event not reaching clients

issam boughanmi 1 Reputation point
2021-08-10T00:30:20.907+00:00

Hi,
here the related code portion that have an issue

hubconnection.On<ConsRequest>("Rcv_ConsPatReady", async (ConsRequest item) =>
{
if (item != null)
{
var c = await service.AddOrUpdateConsultation(item.PatientID,
item.DoctorID,
item.OperatorID,
item.Specialite,
item.Motif);
if (c != null)
{
await hubconnection.SendAsync("Snd_NewConsStarted", c.ID);
string url = "/Consultation/NewCons/" + c.ID.ToString();
await jsRuntime.InvokeAsync<object>("open", url, "_blank");
}
}
});

with the help of some break point the Snd_NewConsStarted call reach the hub but not the other clients .
no errors or exception but it seem that there is some general "silent" SignalR exception because other calls stop working as well .

if i call

await hubconnection.SendAsync("Snd_NewConsStarted", c.ID);

from the outside like on a button click for exampleit works correctly and i can reach the hub and the other clients , but not inside the signalr on event .

any idea on wha't happening please .

thanks .

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,514 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,456 Reputation points Microsoft Vendor
    2021-08-10T07:39:08.393+00:00

    Hi @issam boughanmi ,

    The issue might relate that the Rcv_ConsPatReady function is not called and might be the issue relate the if condition (if (c != null)).

    Try to set a break point the in Rcv_ConsPatReady function and check them?

    Besides, I suggest you could refer the Blazor Official document: Use ASP.NET Core SignalR with Blazor

    Generally, in the Hub class, the SendMessage method can be called by a connected client to send a message to all clients.:

    public class ChatHub : Hub  
    {  
        public async Task SendMessage(string user, string message)  
        {  
            await Clients.All.SendAsync("ReceiveMessage", user, message);  
        }  
    }  
    

    And in the client side, using the HubConnection.On() method to register a handler that will be invoked when the hub method with the specified method name is invoked.

    protected override async Task OnInitializedAsync()  
    {  
        hubConnection = new HubConnectionBuilder()  
            .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))  
            .Build();  
    
        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>  
        {  
            var encodedMsg = $"{user}: {message}";  
            messages.Add(encodedMsg);  
            StateHasChanged();  
        });  
    
        await hubConnection.StartAsync();  
    }  
    
    async Task Send() =>  
        await hubConnection.SendAsync("SendMessage", userInput, messageInput);  
    

    From the above code, we can see that, in the button click event (Send), it will call the hub's SendMessage method first, then in the SendMessage method, using the Clients.All.SendAsync method to call client-side ReceiveMessage method and send message to all clients.

    So, in your scenario, we should ensure the Rcv_ConsPatReady function is called first, then check the if condition.

    And based on your description, I have also created a sample, you can refer it:

    121933-blazorpagecode.txt

    and the ChatHub.cs

    public class ChatHub: Hub  
    {  
        public async Task SendMessage(string user, string message)  
        {  
            await Clients.All.SendAsync("ReceiveMessage", user, message);  
        }  
    
        public async Task SendNotification(string user)  
        {  
            var message = "send you one notification";  
            await Clients.All.SendAsync("ShowNotification", user, message);  
        }  
    }  
    

    In this sample, in the chathub.cs, we defined two methods: SendMessage and SendNotification, in the Client side, in the ReceiveMessage function, we can call the SendNotification method to send notification (since there have two clients, it will show the notification twice). The result as below:

    121875-1.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.