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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With