This is not directly possible with public managed APIs in .NET Core. Environment.UserInteractive
is defaulted to a fixed value and checks to test how stdout is wired up can yield unexpected results as well for various situations (like subprocesses with & without stdout wiring).
In my sample implementation for service hosting APIs i ended up requiring the service registration to add a command line argument (--run-as-service
) when the executable is installed as a windows service (in this app, wen called with --register-service
). A lot of non-.net applications work this way, including docker.