question

issamboughanmi-6975 avatar image
0 Votes"
issamboughanmi-6975 asked ZhiLv-MSFT answered

Signalr netCore 5 Sendasync inside an On Event not reaching clients

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 .







dotnet-aspnet-generaldotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @issamboughanmi-6975,

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


blazorpagecode.txt (1.9 KiB)
1.gif (387.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.