EdmundYeung99
EdmundYeung99

Reputation: 2511

SignalR "Start must be called before data can be sent" when responding to a server request

I have some SignalR client code that invokes a server method after a request from the server:

        var connection = new HubConnection(_hubAddress);
        var hubProxy = _hubConnection.CreateHubProxy(HubName);
        connection.Start().Wait(TimeSpan.FromMinutes(1));
  ...

        hubProxy.On("Client_Method", (string callerId, string requestIdentifier) =>
        {
           if (connection.State == ConnectionState.Connected)
           {
              // error hapening on the invoke call here, despite 
              // the connection state being connected.  
              // Do I need to call Start here?
              hubProxy.Invoke("Proxy_Method_Callback", callerId, requestIdentifier);
           }
        });

But its throwing an exception:

InvalidOperationException: Data cannot be sent because the connection is in the disconnected state. Call start before sending any data.

But if the hubProxy is connected and open to receive the request, why is it erroring on the invoke?

Upvotes: 3

Views: 15088

Answers (4)

xleon
xleon

Reputation: 6375

I find really weird that you get an error invoking something inside proxy.On() handler, because that handler will execute from server side thus you are connected. Are you sure you get the error in that exact line and not anywhere else?

Short explanation:

SignalR does not handle disconnections. You need to handle them by yourself

Long explanation:

When your client loses connection, SignalR will try to reconnect for about 20 seconds. The state is reconnecting. After that period, it goes to disconnected state and won´t connect ever again.

To handle this situation you must listen to connection state changes, and reconnect yourself when state == disconnected. I found out it´s better to create a new hub connection when doing this, otherwise you may get a wide range of exceptions and weird behaviors.

If you invoke any method in disconnected state your app will crash, so I recommend using always try/catch at invoking.

Normally you will want to re-invoke failed calls once connection is re-established.

I wrote a class helper to handle all the mentioned logic. It´s well tested on ios/android devices.

Upvotes: 7

JohnnyFun
JohnnyFun

Reputation: 4303

I ran into a similar error. I was simply passing the wrong hubname into CreateHubProxy.

Upvotes: 0

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

may be the reason is Invoking Method signature is not matching the method the one which you are calling

hubProxy.Invoke("Proxy_Method_Callback", callerId, requestIdentifier);

HubProxy.Invoke Method (methodname, Object[])

method
Type: System.String
The method to be invoked.
args

Type: System.Object[] The arguments for the method.

_

hub.Invoke("Acknowledge","Say Hello to MainHub");

private void Acknowldege(object state);

Upvotes: 0

Georg.Duees
Georg.Duees

Reputation: 154

As the error quotes your connectionState seems to be disconnected from the hub you are calling. On the client you should first call the Start- function of the hub connention.

So your client connects to the server. Example:

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>(
"UpdateStockPrice", stock =>
 Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)
);
await hubConnection.Start();

SignalR Connection/Hubs-Guide

Hope this helps

Best regards,

Georg

Upvotes: 2

Related Questions