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.