0

Could you please suggest a method for .NET Core which is able to detect in runtime whether the code is executed within a service or not?

I have the working code for .NET Framework but I think it is impossible to port it because it is based on properties unavailable in .NET Core 1.1.

demonplus
  • 5,613
  • 12
  • 49
  • 68
  • You need to brush up on your google skills. A quick search yielded multiple hits, including this: http://stackoverflow.com/questions/207896/c-net-detect-whether-program-is-being-run-as-a-service-or-a-console-applicati – rory.ap May 03 '17 at 11:14
  • Sorry your link is not for .NET Core and that is my problem. I have working code for .NET – demonplus May 03 '17 at 11:16
  • Wouldn't it still apply? Either way, you need to show evidence that you researched this (the number one reason listed on the down vote button); – rory.ap May 03 '17 at 11:16
  • No. I can't port it because most of the properties are unavailable in .NET Core 1.1 – demonplus May 03 '17 at 11:17
  • Like Environment.UserInteractive – demonplus May 03 '17 at 11:17
  • If this is in the context of a library, it's almost always better to *let the caller tell you* or *let the caller supply callbacks* such that you *don't* have to (imperfectly) try to determine if you're running in the context of a service. – Damien_The_Unbeliever May 03 '17 at 12:06
  • @Damien_The_Unbeliever Yes, this is in the context of a library. But probably I can't do what you are suggesting because I can't change the interfaces right now – demonplus May 03 '17 at 12:08
  • .NET Core only supports console and web apps now. It does not have anything like Windows service. – Lex Li May 03 '17 at 12:14
  • @LexLi I think you can host .net core as windows service, look here http://dotnetthoughts.net/how-to-host-your-aspnet-core-in-a-windows-service/ for example – demonplus May 03 '17 at 12:17
  • Or this one: https://www.nuget.org/packages/PeterKottas.DotNetCore.WindowsService/ – demonplus May 03 '17 at 12:18
  • .net core has no ServiceBase so i created an alternative (for which petter kottas has made a nice API on top of). Added an answer based on my experience while implementing https://github.com/dasMulli/dotnet-win32-service – Martin Ullrich May 03 '17 at 12:21
  • @rory.ap this question is valid as it applies to .net core APIs and implementations. I spent some time researching this only to find out that there is no fool-proof way. even the property for Environment.UserInteractive can fail (looked at .net framework reference source, corefx, other implementations like NSSM, libs for GO that docker uses). – Martin Ullrich May 03 '17 at 12:24

1 Answers1

1

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.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217