6

I am developing 2 applications, the first being a C# console application and the other an Asp.net web application. I am using SignalR to connect the two.

This is my C# console application (Client)

public class RoboHub
{
    public static IHubProxy _hub;

    public RoboHub()
    {
        StartHubConnection();
        _hub.On("GetGoals", () => GetGoals());
        _hub.On("PrintMessageRobot", x => PrintMessageRobot(x));

        Thread thread = new Thread(MonitorHubStatus);
        thread.Start();
    }

    public void GetGoals()
    {
        //TODO: Does stuff
    }

    public void PrintMessageRobot(string msg)
    {
        Console.WriteLine(msg);
    }

    public void StartHubConnection()
    {
        Console.WriteLine("Robo Hub Starting");
        string url = @"http://localhost:46124/";
        var connection = new HubConnection(url);
        _hub = connection.CreateHubProxy("WebHub");
        connection.Start().Wait();
        Console.WriteLine("Robo Hub Running");
    }

    public void MonitorHubStatus()
    {
        while (true)
        {
            Thread.Sleep(1000);
            _hub.Invoke("Ping", "ping").Wait();
            Console.WriteLine("WebHub Pinged : " + DateTime.Now);
        }
    }
}

When the console application runs, it creates an instance of the RoboHub class. Which in turn starts a connection to the SignalR hub and on a separate thread starts the method MonitorHubStatus which is something I implemented to check if the C# console application client is still actively connected to the hub.

This is my Web hub (within the Asp.net Web application)

public class WebHub : Hub
{
    /// <summary>
    /// This method should be called by the Web Clients. 
    /// This method should call the method on the robot clients.
    /// </summary>
    public void GetGoalsHub()
    {
        lock (UserHandler.Connections)
        {
            if (UserHandler.Connections.Any(connection => connection.Contains("Robot")))
            {
                Clients.All.GetGoals();
            }
        }
        //TODO add Error method to call on the client
    }

    /// <summary>
    /// Override Methods
    /// </summary>
    /// <returns></returns>
    public override Task OnConnected()
    {
        lock (UserHandler.Connections)
        {
            //Find out who is connecting based on the User-Agent header
            var query = (from r in Context.Headers
                where r.Key == "User-Agent"
                select r).SingleOrDefault().ToString();

            if (query.Contains("SignalR.Client.NET45"))
            {
                UserHandler.Connections.Add("Robot : " + Context.ConnectionId);
            }
            else
            {
                UserHandler.Connections.Add("Web Application : " + Context.ConnectionId);
                GetGoalsHub();
            }
        }
        Clients.All.UpdateConnections(UserHandler.Connections);
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        lock (UserHandler.Connections)
        {
            for (int i = 0; i < UserHandler.Connections.Count; i++)
            {
                if (UserHandler.Connections[i].Contains(Context.ConnectionId))
                {
                    UserHandler.Connections.Remove(UserHandler.Connections[i]);
                }
            }
        }
        Clients.All.UpdateConnections(UserHandler.Connections);
        return base.OnDisconnected(stopCalled);
    }

    public void Ping(string msg)
    {
        Clients.All.PrintMessageRobot("Pong : " + DateTime.Now);
    }
}
public static class UserHandler
{
    public static List<string> Connections = new List<string>(); 
}

Currently the 2 applications seem to work for a time, until after a while this error randomly appears:

Connection started reconnecting before invocation result was received.

Further more should the web hub call any other method on the C# console client such as the GetGoals method. The 'Ping Pong' method freezes and after time a similar exception is thrown. throughout this the web client continues to function perfectly and the web client can communicate back and forth with the hub server.

Can anyone suggest what the issue could be? Edit: Further investigation leads me to believe it to be something to do with threading, however it is difficult to find the source of the issues.

1

3 Answers 3

5

The problem is with the invoke call:

_hub.Invoke("MethodName", "Parameters").Wait();

Here I am telling it to wait for a response however I did not program any reply mechanism in the web server.

This error was fix by switching to:

_hub.Invoke("MethodName", "Parameters");

Now it follows a 'fire and forget' methodology and it now no longer gets the error. Should anyone else get this error be sure to check whether you need a response or not.

2
  • Thanks for taking the time to post your solution. I ran into something similar and this helped.
    – webworm
    Jun 14, 2016 at 13:07
  • 11
    This is not the correct answer. Here, Wait() makes your code wait the execution of the async call (which is Invoke, and which could cause a possible deadlock), and you don't need to program any reply mechanism in your web server, the server will return a response no matter what your hub code does. By removing Wait(), you are just using the fire and forget pattern, by starting an async call and not waiting until it finishes. Doing this is just a bad workaround, which may arise other problems, you should understand signal lifecycle better and understand why your code is broken. Aug 17, 2017 at 13:30
3

You will get the same error message if the data being sent to server side is a 'Non-serializable' (e.g.) List of business objects which don't have [Serializable] attribute

3

I got this same exception when the payload was to big. I fixed it by changing the following line, in the Startup declaration for signlr.net. It removes the size limit

GlobalHost.Configuration.MaxIncomingWebSocketMessageSize = null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.