How can I know when Kestrel has started listening?

后端 未结 4 1155
旧时难觅i
旧时难觅i 2021-02-15 13:10

I need to notify systemd that my service has started up successfully, and a task it needs to run after startup requires that the server is already listening

4条回答
  •  梦毁少年i
    2021-02-15 14:14

    This is what I ended up going with, for now. Seems to be working fine:

    host.Start();
    
    Log.Information("Press Ctrl+C to shut down...");
    Console.CancelKeyPress += OnConsoleCancelKeyPress;
    
    var waitHandles = new WaitHandle[] {
        CancelTokenSource.Token.WaitHandle
    };
    
    WaitHandle.WaitAll(waitHandles);
    Log.Information("Shutting down...");
    

    Then, in the Ctrl+C event handler:

    private static void OnConsoleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
        Log.Debug("Got Ctrl+C from console.");
        CancelTokenSource.Cancel();
    }
    

提交回复
热议问题