2
votes

I recently created a proof of concept console application using SignalR (self host). It worked a treat for our use. The client connected fine and I was able to send updates from the server to the client. Lovely!

I've now transferred the code from the Console application to a winforms application for a prettier UI. Now that same client won't connect to the server yet it will still connect to the old Console version.

Winforms code:

string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                // Let the app know the server is up
            }

Console code:

string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }

Client connection code:

if (!connected)
                {
                    int i = 0;

                    // Try 3 times
                    while (i <= 2)
                    {
                        try
                        {
                            string server = Properties.Settings.Default.Server + ":" + Properties.Settings.Default.PortNumber.ToString();
                            connection = new HubConnection(server);
                            connection.StateChanged += connection_StateChanged;

                            hub = connection.CreateHubProxy("MyHub");
                            connection.Start().Wait();

                            hub.On<string>("addMessage", param => { UpdateAlarmStatus(param); });
                            return true;
                        }
                        catch (Exception)
                        {
                            i++;
                        }
                    }
                    return false;
                }
                else
                {
                    return true;
                }

The error the client is reporting is: Exception:Thrown: "No connection could be made because the target machine actively refused it" (System.Net.Sockets.SocketException) A System.Net.Sockets.SocketException was thrown: "No connection could be made because the target machine actively refused it" Time: 25/01/2015 15:09:23 Thread:Worker Thread[8232]

Why would the target machine (localhost) refuse itself which the Console version doesn't? I've been looking at the code over and over and I cannot see where I'm going wrong. Can anyone point me in the right direction please?

Thank you for reading. Paul.

1
PS. I'm obviously not running the two server projects at the same time! - paulpitchford
can i just ask, why are you running a signalR server in winforms? - Nikola.Lukovic
The IT admin at this client will not give us access to IIS for some reason and we need to do more with the app than a console application will allow. - paulpitchford
than, maybe you can have two projects here? winforms client and a signalr console server running in a service in a background? - Nikola.Lukovic
It's preferable for the server not to be a console application as it's monitoring a .NET based control board which is monitoring the resources we're sending to the client via signalR. Occasionally an administrator will remote into the server to tweak settings which we're hoping to control using a forms based application. - paulpitchford

1 Answers

0
votes

I suspect this is an issue with the configuration of your machine/infrastructure rather than the code itself, which looks fine at first glance.

Have you checked the console debug output in Visual Studio? I recently encountered an issue with similar symptoms and that was what gave me the initial clue to keep investigating. In my particular case, an exception was written to the console debug output that didn't make it to the client.

SignalR will normally negotiate with the server automatically to determine the best transport method to use. In a .NET client, the available options are LongPollingTransport, ServerSentEventsTransport and WebSocketTransport. So for some reason, your console app can use at least one of those methods, whereas your WinForms client cannot.

You can perhaps enable tracing to give you more information to work with. To do this, enter the below before you create the hub proxy:

hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = Console.Out;