Saturday, March 13, 2010

Cannot Start Service from the command line or debugger. A Windows Service must first be installed (using installutil.exe)..

When you create a new Windows Service project and try to debug it, Visual Studio will show you a Windows Service Start Failure with the message "Cannot Start Service from the command line or debugger. A Windows Service must first be installed (using installutil.exe) and then started with the Server Explorer, Windows Services Administrative tool or the NET START command.".

The trick my team and I use to workaround this problem, makes use of the service Debug flag. If the Debug flag is on, we just start the service by using our own public Start method. When the OnStart event is fired in the service itself, we call the same public Start method.

This goes in your service.

   1:  protected override void OnStart(string[] args)
   2:  {
   3:       Start();
   4:  }
   6:  public void Start()
   7:  {
   8:       //Start!
   9:  }

And this goes in Program.cs.

   1:  namespace UdpListener
   2:  {
   3:      static class Program
   4:      {
   5:          /// <summary>
   6:          /// The main entry point for the application.
   7:          /// </summary>
   8:          static void Main()
   9:          {
  10:  #if (!DEBUG)
  11:              ServiceBase[] ServicesToRun;
  12:              ServicesToRun = new ServiceBase[] 
  13:              { 
  14:                  new UdpListener() 
  15:              };
  16:              ServiceBase.Run(ServicesToRun);
  17:  #else
  18:              UdpListener listener = new UdpListener();
  19:              listener.Start();
  20:  #endif           
  21:          }
  22:      }
  23:  }

You can set the Debug flag of your service in your service properties.

The only problem with this solution is that you can't debug your OnStop event but this hasn't been an issue for us so far.

Other ways to solve this issue can be found here:
- MSDN: How to: Debug Windows Service Applications
- KB824344: How to debug Windows services
- MSDN: How to: Launch the Debugger Automatically


  1. It helped me. Thanks.

  2. Goood one short and useful!

  3. Super.. It's useful for me..

  4. I have window 7 ,if i am starting a setup same error is occuring ...please help ,,,

  5. Anyone know how to do this in VB?

  6. Error on .Start()

    My Code:

    HKWFService listener = new HKWFService(); //my service file name

  7. This comment has been removed by the author.

  8. I try to call my application through a scheduler.
    And I still get this error.