1

Older .NET framework Windows service applications could not be executed in any other way other than as a registered service on a Windows machine but I've noticed that .NET worker service applications even if implementing a Windows service (UseWindowsService) and being registered as a service can still be started as standard console applications by double clicking on them. Is there a way to prevent this from happening?

This is what older .NET framework Windows service applications displayed at any attempt to start them not as a Windows service.

enter image description here

whatever
  • 2,492
  • 6
  • 30
  • 42
  • Why do you want this? There's no built-in restriction on Windows that prevents services from running on the command line. .NET Framework checked whether the application was running as a service explicitly. .NET Core is cross-platform though and can't use Windows-specific features. `UseWindowsService` maps service commands like Start/Stop to application events, it doesn't *install* the service directly. – Panagiotis Kanavos Mar 29 '23 at 10:11
  • Check [Detect whether program is being run as a service or a console application](https://stackoverflow.com/questions/207896/c-net-detect-whether-program-is-being-run-as-a-service-or-a-console-applicati). There are several ways to detect if an application runs as a service. The distinction may not be that important though, when services are often packaged and deployed as containers – Panagiotis Kanavos Mar 29 '23 at 10:15
  • You seem to read and regardless of what is written follow you own thread of thoughts, I never asked anything about how to install the application as a service did I ? :) What I'm asking about existed built-in when creating .NET framework based windows services so is it really that difficult to understand why I'm asking how to achieve the same behavior ? – whatever Mar 30 '23 at 13:18
  • Did anyone else answer? If this was a good and clear question you'd get many answers. The question is unclear though and the only direct answer it can get is `You can't`. .NET 7 is .NET *Core* 7, a completely different, cross-platform runtime. You'll have to find a way to solve your original problem. That's why I asked what you really want and tried to think of valid problems and how to solve them. I explained that this behavior isn't available, why it's not available and that you can emulate it if you want by detecting whether the application runs as a service or not. – Panagiotis Kanavos Mar 30 '23 at 13:41

0 Answers0