Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: Connection was disconnected before invocation result was received

Tags:

c#

signalr

In one of my WCF-Services (stateless), I want to send a message via SignalR. Because the service is stateless, and the hub is on another machine, I connect to SignalR, send the message, and disconnect.

proxy.Connect().Wait();
proxy.SendMessageToUsers(receiverUserNames, message).Wait();
proxy.Disconnect();

From time to time, there are InvalidOperationExceptions (Connection was disconnected before invocation result was received).

I understand from this post (C# SignalR Exception - Connection started reconnecting before invocation result was received) that .Wait is not a good idea. But I think I need to wait for Connect and SendMessage to complete, before disconnect.

So, what else can I do?

Best regards, Stefan

like image 267
Stefan Avatar asked Oct 27 '25 19:10

Stefan


1 Answers

The error makes sense because the code is synchronous. Thus Disconnect may be called before invocation result is received.

What about...

Task.Factory.StartNew(async() => {
    await proxy.Connect();
    await proxy.SendMessageToUsers(receiverUserNames, message);
    await proxy.Disconnect();
});

That way you are making sure proxy.Disconnect() will not be called until message is sent.

like image 70
xleon Avatar answered Oct 29 '25 09:10

xleon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!