7

How can I detect I'm running under mono-service2? (in C#/.NET 3.5, running mono 2.6.7 on ubuntu 11)

Type.GetType("Mono.Runtime") tells me I'm running in mono, so that part is ok. But Environment.UserInteractive is always false under mono it seems, so I'm struggling to figure out if I'm actually running under mono-service2 - with no console/terminal.

BaBu
  • 1,923
  • 2
  • 16
  • 27

2 Answers2

5

Environment.UserInteractive is the proper solution but, unfortunately, it is currently not implemented in Mono. I may take a look on it someday and upgrade this answer ;)

However just to detect you can probably use hacky solution around this one: name given to mono-service in m parameter will become the friendly name of application domain of that service (at least according to the current source code of mono-service). So, when run with this parameter you should be able to test:

AppDomain.CurrentDomain.FriendlyName == "NameGivenToMParameter"

If it is true, then your application is apparently ran with mono-service (with given parameter). Print the value of application domain name to file to see if it really works (it does for me) ;) I do not know if it really resolves your problem.

konrad.kruczynski
  • 46,413
  • 6
  • 36
  • 47
  • Tnx. The 'FriendlyName' hack works fine. But... It's not ideal (so I leave this question unanswered for a while longer :). Messing with FriendlyName affects my logging and it would be better to not having to remember the -m option to mono-service. But hey; thanks anyway. I'll figure something out. And now I at least _have_ a solution. – BaBu Jan 31 '12 at 07:54
  • I found a way to figure out whether there is a console/terminal available or not under mono on linux: 'Mono.Unix.Native.Syscall.isatty(0)'. This won't work on windows but there of course Environment.UserInteractive works. – BaBu Feb 02 '12 at 10:39
  • 1
    @BaBu: yeah, good idea. when v4.5 is available, this will be useful too: http://msdn.microsoft.com/en-us/library/system.console.isoutputredirected(v=vs.110).aspx – konrad.kruczynski Feb 02 '12 at 16:10
0
if (Environment.OSVersion.Platform.Equals(PlatformID.Unix)) 
{
}
Ran Cohen
  • 486
  • 1
  • 3
  • 10