I find I am learning more and more about Windows Services and in doing so I have found the need to revisit the #if(!DEBUG) hat trick that I orginally read about in the book I've been reading (C# 4.0 in a Nutshell) but which I thought was too ghetto to take seriously. It turns out that you need to use it like so in testing a Windows Service which basically seems to be a console application which uses the ServiceBase type somewhere within.
#if(!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new FooService() };
ServiceBase.Run(ServicesToRun);
#else
FooService myService = new FooService();
myService.PublicMethodOnFooService();
#endif
If you fail to use this hack and you just try run the debugger you get this:
Cannot start service from the command line or a debugger. A Windows Service must be installed (using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command.
No comments:
Post a Comment