Site Search:
Sign in | Join | Help
4Penny.net

VB.NET

Notes, Tricks and Tips on VB.NET

RUN my Windows Service without installing and starting it

I am often asked the question "Can't I just RUN my Windows Service without installing and starting it?".

My answer usually is "No, you can't but..."

There is a "but": you can create your Windows service as a hybrid application, so it will run as a console application also.

Why would you want to do that?
• It makes debugging from within Visual Studio a breeze
• Sometimes you want to show some debugging information in a production environment by using Console.WriteLine()
• You don't always want to install your service to see if it runs in a particular environment
Turning a .NET Windows service into a hybrid application is actually very simple. In your Main() method you add the following:
static class Program
{
  static void Main(params string[] parameters)
  {
    if (parameters.Length > 0 && parameters[0].ToLower() == "/console")
      new MyService().RunConsole(parameters);
    else
      ServiceBase.Run(new ServiceBase[] { new MyService() });
   }
}
Then, in your service class, add a RunConsole() method:
   public void RunConsole(string[] args)
   {
      OnStart(args);

      Console.WriteLine("Service running... Press any key to stop");
     
      Console.Read();
   
      OnStop();
   }
That's all there is to it. To run your service as a console app, just specify "/console" as the first paramter when running the .EXE.

 

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.