Windows or Console application target for Windows Service?
-
When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:
static void Main(string\[\] args) { ServiceBase\[\] ServicesToRun; TestService srv = new TestService(); ServicesToRun = new ServiceBase\[\] { srv }; if (Environment.UserInteractive) { char KeyChar; srv.ManualStart(args); Console.WriteLine("Service started in console mode, press Space key to stop"); Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)"); while (true) { KeyChar = Console.ReadKey().KeyChar; Console.WriteLine(""); if (KeyChar == ' ') break; else { srv.ManualCustomCommand(KeyChar + 128); } } srv.ManualStop(); } else ServiceBase.Run(ServicesToRun); }
In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?
-
When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:
static void Main(string\[\] args) { ServiceBase\[\] ServicesToRun; TestService srv = new TestService(); ServicesToRun = new ServiceBase\[\] { srv }; if (Environment.UserInteractive) { char KeyChar; srv.ManualStart(args); Console.WriteLine("Service started in console mode, press Space key to stop"); Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)"); while (true) { KeyChar = Console.ReadKey().KeyChar; Console.WriteLine(""); if (KeyChar == ' ') break; else { srv.ManualCustomCommand(KeyChar + 128); } } srv.ManualStop(); } else ServiceBase.Run(ServicesToRun); }
In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?
I would arrange all the stuff that you want to run as the service into another class project. Then you can have the Windows Service project reference to this project for actual usage, and create console app project or windows app project that reference the class project for debugging.
-
When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:
static void Main(string\[\] args) { ServiceBase\[\] ServicesToRun; TestService srv = new TestService(); ServicesToRun = new ServiceBase\[\] { srv }; if (Environment.UserInteractive) { char KeyChar; srv.ManualStart(args); Console.WriteLine("Service started in console mode, press Space key to stop"); Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)"); while (true) { KeyChar = Console.ReadKey().KeyChar; Console.WriteLine(""); if (KeyChar == ' ') break; else { srv.ManualCustomCommand(KeyChar + 128); } } srv.ManualStop(); } else ServiceBase.Run(ServicesToRun); }
In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?
Windows Services have no GUI so Winexe makes no sense. I always compile service programs as console, but more to the point, I compile the actual service code in a DLL which the program calls. I do pretty much what you show there and control my services from the command line. Edit: When I write an application that can be used in console or WinForms mode, I compile it as console.
modified on Monday, July 26, 2010 1:21 PM