5

Is there any way to stop SignalR server? I have self-hosted SignalR server as Windows service but i can't find any way to stop server/service.

I tried this but it doesn't work - server keeps listening and prevents service from stopping.

Alternatively, how can i stop service altogether, forcing SignalR shutdown?

[edit]:

Most of source i cannot share (copyright/security) but I'll do my best:

SignalR server init

Task signalRTask = null;
IDisposable SignalR;

#region SignalR server init
// Kreiraj SignalR server
try
{
    cancelTokenSrc = new CancellationTokenSource();
    signalRTask = Task.Factory.StartNew(RunSignalR, TaskCreationOptions.LongRunning, cancelTokenSrc.Token);

    logfile.Info("Starting notifications pool thread...");
    //Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy. HH:mm:ss ") + "Starting notifications pool thread...");
    senderThread = new Thread(delegate()
    {
        sender.poolEvents();
    });

    senderThread.Start();
}
catch (Exception ex)
{
    // greška u startanju SignalR servera
    ServiceEngine.logfile.Info("Error starting SignalR on " + signalr_bind + " with error:" + ex.ToString());
    //Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy. HH:mm:ss ") + "Error starting SignalR @ {1} with error {0}", ex.ToString(), signalr_bind);
}
#endregion

#region SignalR start
// pokreće signalR server
public void RunSignalR(object task)
{
    try
    {
        logfile.Info("Starting SignalR server on " + signalRBindAddr);
        SignalR = WebApp.Start(signalRBindAddr);

        logfile.Info("SignalR server running on " + signalRBindAddr);
    }
    catch (Exception ex)
    {
        ServiceEngine.logfile.Info("Error starting SignalR: " + ex.ToString());
    }
    //Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy. HH:mm:ss ") + "SignalR server running on {0}", signalRBindAddr);
}
#endregion

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

public void Stop()
{
    try
    {
        logfile.Info("Stop called...");
        SignalR.Dispose();
        cancelTokenSrc.Cancel();
        //signalRTask.Dispose();
        logfile.Info("SignalR down, exit...");
        //Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy. HH:mm:ss ") + "SignalR down, exit...");
    }
    catch (Exception ex)
    {
        ServiceEngine.logfile.Info(ex.ToString());
    }
}

When i call Stop(), SignalR keeps listening (clients can connect, port has status "LISTEN"...) and i can't stop service.

P.S. Sorry for bad formatting and bad English

CSDev
  • 3,177
  • 6
  • 19
  • 37
nighthawk
  • 773
  • 7
  • 30

1 Answers1

0

Not clearly a complete answer, but I managed to achieve a pause, not a stop in my ancient services as explained in Self-Hosting SignalR in a Windows Service.

However, I then refactored all of them to be micro services, placing the hubs which must be stopped in separate micro services. This solution allows you to manually start and stop the services via SCM or via direct invocation if you are running on a container.

Architecturally speaking, the latter solution has a certain overhead as you are adding a new layer which requires authentication, federation and data IO. Depending on the context, you might already have the infrastructure to support that.

Yennefer
  • 5,704
  • 7
  • 31
  • 44