views:

353

answers:

4

How to run .NET services from command line? net start "Sample Service" is not working.

+3  A: 

Try using sc start

SwDevMan81
+7  A: 

Services are not supposed to be started from command line. First register it (installutil /i service_path) then start with net start or sc start.

Alternatively, create Main entry point in service project and start your logic, so you can test your service from command line. However this won't be real service environment.

aloneguid
+1 for the comment about having a main entry point in the project for INITIAL DEVELOPMENT. As Ivan says the service can behave differently when run as a service (different credentials).
Eric J.
Every service I've ever written supports a command-line parameter that makes it run as a console-mode application. I use /run but whatever you use, I fully recommend this sort of pattern. It'll save you hours of frustrating debugging.
Mark
A: 

You can start or stop a service by calling:

net start "Service Name"
net stop "Service Name"

For details, see this technet article.

This should work, provided the following are true:

  • The command prompt/user has the appropriate permissions to start the service. This will be required for ANY solution.
  • The service is installed and registered correctly with the system. My suspicion is that this may be the culprit. Is the service listed under services?
Reed Copsey
The OP already tried this (and was in the initial revision).
Peter Mortensen
I'll edit my answer to provide more details.
Reed Copsey
+1  A: 

Make sure that you add an installer to your Windows service. You have to do this in order for the InstallUtil.exe utility to work. You can see how to that here.

After you add the installer, InstallUtil.exe can be used to install and uninstall your service.

  • Install: InstallUtil.exe <YourServiceExecutable>
  • Uninstall: InstallUtil.exe /u <YourServiceExecutable>

To start and stop your service, use Reed's solution.

If you want to get fancy, you can add some command-line logic to your service that will allow you to do all of this (install-and-start/stop-and-uninstall) directly from your service, i.e., you won't have to use InstallUtil.exe anymore. That solution is here.

Matt Davis