Obviously, it's the Main() above that ends up executing the service, and it's the Main() that this approach manipulates so that the Windows Service can be debugged directly within Visual Studio .NET.
Using the example above (and removing some of the comments), here's how:
// The main entry point for the process
static void Main()
{
#if (!DEBUG)
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
// debug code: allows the process to run as a non-service
// will kick off the service start point, but never kill it
// shut down the debugger to exit
Service1 service = new Service1();
service.<Your Service's Primary Method Here>();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}
本文介绍了一种在Visual Studio.NET中直接调试Windows服务的方法。通过修改服务的Main()入口点,可以在调试模式下作为非服务进程运行,便于开发者进行调试。
1705

被折叠的 条评论
为什么被折叠?



