How to detect if Topshelf is running in console mode

前端 未结 2 1437
星月不相逢
星月不相逢 2021-02-13 13:21

I am using Topshelf combined with FluentSchedule for a Windows Service.

However, I want to be able to trial-run the application to simply start up and not execute the Fl

2条回答
  •  遇见更好的自我
    2021-02-13 13:54

    It's sort of a hack, but you can try to cast the HostControl interface to ConsoleRunHost, and if it's that type, you're running as a console application.

    It's not ideal, sure, but surely you can hide this in an extension method to make it less ugly.

    public static bool IsRunningAsConsole(this HostControl control)
    {
        return control is ConsoleRunHost;
    }
    

    And you then get access to the HostControl by passing it in through the call to WhenStarted() in your service configuration.

    s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
    

提交回复
热议问题