Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if Topshelf is running in console mode

Tags:

topshelf

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 FluentSchedule code that sets up the timer etc.

Is there a way when running the exe file from the Command Line (i.e. without 'install' command) to check from TopShelf that it is running in Console mode?

like image 985
Redeemed1 Avatar asked Feb 26 '15 11:02

Redeemed1


1 Answers

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));
like image 150
Chris Patterson Avatar answered Oct 21 '22 10:10

Chris Patterson