Windows Service Start Failure error message

Ever encountered the Windows Service Start Failure error message when trying to debug a Windows Service?

 Cannot start service from the command line or a debugger. A Windows Service must first be installed (using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command.

Windows Services are the best choice for scheduled processes running on an application server. However, they are a pain to debug since you can only run them under the context of a service. There are a series of steps for attaching to a Windows Service process for debugging, which means you can spend a lot of time during development just to debug your code and get it right. Here’s a easy alternative that allows you to develop a Windows Service interactively using debugging, and also publish and run the service without any code or settings changes at all.

This tutorial steps through creating an example Windows Service that simply writes to the Event Log to prove it runs successfully.

  1. Create a new Project of type Windows > Windows Service.
  2. Delete the Program.cs class. This contains the Main() method, which we will be relocating to the service class. Delete the method from the Program.cs class.
  3. Open the service class (Code view). Insert the following code:
    • using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Diagnostics;
      using System.Linq;
      using System.ServiceProcess;
      using System.Text;namespace WindowsService1
      {
      public partial class Service1 : ServiceBase
      {
      public Service1()
      {
      InitializeComponent();
      }static void Main()
      {
      if (System.Diagnostics.Debugger.IsAttached)
      {
      Service1 service = new Service1();
      service.OnStart(null);
      }
      else
      {
      ServiceBase[] ServicesToRun;
      ServicesToRun = new ServiceBase[]
      {
      new Service1()
      };
      ServiceBase.Run(ServicesToRun);
      }
      }protected override void OnStart(string[] args)
      {
      EventLog.WriteEvent("Service1", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service ran successfully." });
      }protected override void OnStop()
      {
      }
      }
      }
  4. Open the service class (Design view).
  5. Right-click and select Add Installer. This simply adds the necessary components for service installation and registration.
  6. Select Debug > Start Debugging to run with debugging. You can place a breakpoint in the OnStart and step through any code as you would expect from a standard Windows Application.
  7. Build the project.
  8. Install the service using InstallUtil.exe (the tool is located in %WINDIR%Microsoft.NETFrameworkv4.0.XXXXX).
  9. Run the following command to install the service:
    • installutil windowswervice1.exe
  10. Run the following command to start the service:
    • net start service1
  11. Check the event logs under Windows Logs > Application. You can confirm the service ran successfully by finding the Information level message from the Service1 source.

By using this as a template for all your Windows Services, you can ensure easy and pain-free debugging during initial and ongoing development!

Like this post? Share it!