The following article describes how to create a Windows Service in C# that can be started from the command line as well as being registered as a service. This is extremely useful for debugging purposes both in development environments as well as on remote servers.

Part 1: Add property to server class

The first step is to have a way to notify the service that it is being run from the command line. This is done by adding the following property to the service class.

private bool fromCmdLine = false;   
 
public bool FromCmdLine
{ 
    get { return fromCmdLine; }   
    set { fromCmdLine = value; }   
}

Part 2: Add method to start server to server class

Once the property has been added it can be referenced from the Main() method later. Another method is required to start the actual service when running in command line, and therefore we will also add the following method.

public void Start(string[] args)
{
     this.OnStart(args);
}

The last modification to the server class is to check whether it is running in command line and if so to wait indefinitely for request to come through.

protected override void OnStart(string[] args)
{
    // Process your server on start code here...    
    // Check if the service is run from command line    
    if (FromCmdLine)   
    {   
        // Force Service to Start if running Command Line    
        Thread.Sleep(Timeout.Infinite);
    } 
}

Part 4: Modify Main() method

The following code sample shows the implementation of the service start-up module to allow for command line parameters, and check for debug mode. The compiled exe does a self check to determine if it is starting up as a service, or from the command line by passing the “-id” parameter. When running from the command line it will act like a service and allow for the debugging of the code.

///     
/// Server Startup Method with Command Line Parameters    
///     
/// Optional Command Line    
static void Main(string[] args)   
{   
     /// Create a service base array to load services    
     ServiceBase[] objServicesToRun;   
     /// Create an instance of the service you want to run   
     MyServiceClass objServer = new MyServiceClass();  
     /// Pass a boolean flag to the service to force starting from commandline,   
     /// assume this is false   
     objServer.FromCmdLine = false;  
     /// Process command line options if they exist   
     if (args.Length > 0)  
     {  
         /// Loop through all options supplied in command line using foreach   
         foreach (string strCmdOption in args)  
         {  
             /// Check for debug option and set commandline to true for server   
             /// else set to false and pass parameters   
             switch (strCmdOption)  
             {  
                 case "-d":  
                     objServer.FromCmdLine = true;  
                     break;  
             }  
         }  
     }  
     if (objServer.FromCmdLine)  
     {  
         /// If no command line was supplied start windows service   
         objServicesToRun = new ServiceBase[] { new MyServiceClass() };  
         ServiceBase.Run(objServicesToRun);  
     }  
     else  
     {  
         /// Start as non windows service   
         objServer.Start(new string[] { "", "" });  
     }  
}
To change enable debug mode in Visual Studio add the “-d” parameter to the Command Line Arguments field under Solution –> Debug.