Mufasa wrote:
I used to work with somebody who wrote services that if you ran them from a
console prompt with a -console it would run writing things to the screen as
a normal console program. I'm sure he programmed this himself but I was
wondering if anybody could give me guidance on how to do that.
public static void Main() {
using (var s = new Service1()) {
if (Environment.UserInteractive) {
s.OnStart();
Console.ReadLine();
s.OnStop();
} else {
ServiceBase.Run(s);
}
}
}
Or something along those lines. You don't need a separate command-line
option, unless you *really* plan on running the application in a
non-interactive console window as well.
What's much more fun is combining this with a command-line parser to install
your service when somebody passes /i, for example (rather than requiring
them to trot out InstallUtil). Doing this requires some mucking around with
ServiceProcessInstaller.
--
J.