Question: Is it possible to determine if a .Net core console application is running in user interactive mode?

In previous versions of .Net it was possible to test `Environment.UserInteractive` to see if the user had access to the application. This doesn't seem to exist in .Net core.

Answer: 
The problem that .NET Core faces more than classic .NET Framework is how "user interactive" is defined. Even on "classic" .NET, the `Environment.UserInteractive` feels hacky as it relies on a [system API call to query user object flags](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683238(v=vs.85).aspx) and tests for a flag described as [`Window station has visible display surfaces`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686892(v=vs.85).aspx). It is unclear what exactly this should semantically mean on a GUI-less windows nano server that you use to run commands from.

My suggestion is to determine the exact use case on what you want to test. You could for instance test if the attached input and output streams are redirected using `Console.IsOutputRedirected` and `Console. IsInputRedirected`. On non-windows systems, a call to `isatty()` could be made but that isn't currently available as .NET API (you'd have to write the PInvoke code). If you want to determine if you are running as a windows service, TopShelf [checks if the process has been started by the service host](https://github.com/Topshelf/Topshelf/blob/956325c8104136af7e8f8fc708b6237668d1c55c/src/Topshelf/Runtime/Windows/WindowsHostEnvironment.cs#L142-L147). Another approach would be to add additional arguments for specific use cases - e.g. add and check for `--noninteractive` when you want to run a tool from a script.
Another option is to check a process session id: for services it's always 0. So you can use the following snippet:

```cs
// Environment.UserInteractive is not working on NET Core 3.1
if (Process.GetCurrentProcess().SessionId != 0) 
{
    service.RunAsConsole(args);
}
else
{
    ServiceBase.Run(service);
}

```