nighthawk nighthawk - 28 days ago 12
C# Question

Self-Hosted SignalR service won't work (start)

I have SignalR server as Class Library Project and i referenced it in Console application (to simulate Windows service)

Here is code for SignalR

public void Start()
{
try
{
string url = @"http://*:8081";
using (WebApp.Start<Startup>(url))
{
Logger.Info(string.Format("Server running at {0}", url));
}
}
catch (Exception ex)
{
Logger.Exception(ex, "Signalr start");
}

Run = true;

Logger.Info("Starting Worker");
workerThread = new Thread(() =>
{
Worker();
});

workerThread.Start();
}


And here is Startup class

public class Startup
{
Microsoft.AspNet.SignalR.HubConfiguration hubconfiguration = null;

public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
hubconfiguration = new HubConfiguration();
hubconfiguration.EnableDetailedErrors = true;
app.MapSignalR(hubconfiguration);
}
}


So, it is in one thread, and worker is in another. That seems fine since i did it in other project where it works. Worker thread isn't problem, it's just empty loop, not related to server in any way.

Problem is that server seems to "stop" - when i look with Netstat, nobody is listening on port 8081. There is no exception, it just silently fails.

I referenced Owin.Cors (and Owin.Host.HttpListener) in console project that actually runs this server but as I said, server just stops.

When I try to connect, client says "connection actively refused" and Putty (telnet) also says "can't connect".

Where is the problem? In a nutshell, i have Class Library with SignalR server that is referenced in Console project that runs it but server just wont work.

[edit]

And there is code of Console app that starts service

static void Main(string[] args)
{
ServiceEngine Engine = new ServiceEngine();
Engine.Start();
Console.ReadKey();
Engine.Stop();
}


P.S. Sorry for my bad English.

Answer

Well, i solved it. Here was a problem:

    public static void Start()
    {
        try
        {
            string url = @"http://127.0.0.1:8081";
            WebApp.Start<Startup>(url);
            Logger.Info(string.Format("Server running at {0}", url));
        }
        catch (Exception ex)
        {
            Logger.Exception(ex, "signalr start");
        }

        Run = true;

        Logger.Info("Starting Worker");
        workerThread = new Thread(() =>
        {
            Worker();
        });

        workerThread.Start();
    }

As you can see, using statement was removed and now it works fine! Interesting note - you can also make Singleton implementation of this "Engine", and it will also work.