13

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?

Travis
  • 9,959
  • 1
  • 25
  • 46
Redeemed1
  • 3,786
  • 7
  • 33
  • 59
  • I would ask on the mailing list, https://groups.google.com/forum/#!members/topshelf-discuss, if you haven't already. I don't think there is an obvious way. Also you can open up an issue on GitHub: https://github.com/topshelf/topshelf – Travis Apr 07 '15 at 15:05
  • Not related directly with TopShelf, but have a look at http://stackoverflow.com/questions/2397162/how-to-determine-if-starting-inside-a-windows-service – Max Yakimets Apr 09 '15 at 09:40

3 Answers3

18

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));
Darren Gosbell
  • 1,886
  • 13
  • 16
Chris Patterson
  • 16,928
  • 2
  • 34
  • 36
  • 2
    I am having difficulty identifying what and when to reference as a HostControl at runtime. What can I reference prior to calling HostFactory.Run(x => etc.) which will be a HostControl? Or am I doing it too early? Should I check this in the service class I am instantiating in the x.Service and once again, what can I reference in that class that would be a HostControl? – Redeemed1 May 07 '15 at 15:21
  • You are doing it too early. The HostControl is inside your actual service code. The fluent schedule code you mention should not be created outside of the service being started by Topshelf. Otherwise it gets started for install, uninstall, and other Topshelf commands. – Chris Patterson May 07 '15 at 16:51
  • 1
    Chris, I don't feel it's a "hack" at all but rather straightforward. Thanks! – howardlo May 11 '16 at 21:22
  • Works, and I'm with @hlo- not a hack in my book! – Sudhanshu Mishra Oct 19 '16 at 05:55
  • 5
    This doesn't answer the question. Its just a piece of code. Sigh. – Phill Nov 20 '16 at 07:03
  • See this related question: https://stackoverflow.com/q/31062562/204690 for where to add this snipnet - i.e. in the WhenStarted() overload – Grhm Dec 01 '17 at 11:33
  • @Phill How does it not answer the question? – user247702 Jan 12 '18 at 08:10
  • @Phill Agree! I do not know how to implement this code :( – Vunb Nov 23 '18 at 07:39
11

You can use Environment.UserInteractive . Technically this won't work in 100% of the cases as it is possible to run a service in user-interactive mode, but this is a fringe case.

Francois Botha
  • 3,434
  • 28
  • 39
0

I always set a service name, display name and service description for my Topshelf services. In the "Path to executable" in the service's Properties window, you can see command-line switches for these things. In other words: when run as a service, the args array will not be empty.

    if (args.Count() == 0 && Environment.UserInteractive)
    {
        // Running in console mode
    }
macnerd
  • 41
  • 5