How to detect if Topshelf is running in console mode
Asked Answered
I

3

15

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?

Innerve answered 26/2, 2015 at 11:0 Comment(2)
I would ask on the mailing list, 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: github.com/topshelf/topshelfDendy
Not related directly with TopShelf, but have a look at #2397662Deangelis
B
19

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));
Brigandage answered 30/4, 2015 at 19:28 Comment(8)
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<T> and once again, what can I reference in that class that would be a HostControl?Innerve
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.Brigandage
Chris, I don't feel it's a "hack" at all but rather straightforward. Thanks!Gerrygerrymander
Works, and I'm with @hlo- not a hack in my book!Zeb
This doesn't answer the question. Its just a piece of code. Sigh.Palladic
See this related question: https://mcmap.net/q/822789/-how-to-pass-hostcontrol-instance-to-custom-host-service-in-topshelf/204690 for where to add this snipnet - i.e. in the WhenStarted() overloadCatechism
@Palladic How does it not answer the question?Nace
@Palladic Agree! I do not know how to implement this code :(Talbot
I
13

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.

Inflationary answered 27/7, 2015 at 11:25 Comment(0)
B
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
    }
Brush answered 3/4, 2021 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.