[英]Running a windows service in a console
What is the best way to run a windows service as a console? 将Windows服务作为控制台运行的最佳方法是什么?
My current idea is to pass in an "/exe" argument and do the work of the windows service, then calling Application.Run(). 我目前的想法是传入一个“/ exe”参数并执行Windows服务的工作,然后调用Application.Run()。
The reason I'm doing this is to better debug a windows service and allow easier profiling of the code. 我这样做的原因是为了更好地调试Windows服务并允许更容易地分析代码。 The service is basically hosting .NET remoted objects. 该服务基本上托管.NET远程对象。
This is how I do it. 我就是这样做的。 Give me the same .exe for console app and service. 为控制台应用和服务提供相同的.exe。 To start as a console app it needs a command line parameter of -c. 要作为控制台应用程序启动,它需要命令行参数-c。
private static ManualResetEvent m_daemonUp = new ManualResetEvent(false);
[STAThread]
static void Main(string[] args)
{
bool isConsole = false;
if (args != null && args.Length == 1 && args[0].StartsWith("-c")) {
isConsole = true;
Console.WriteLine("Daemon starting");
MyDaemon daemon = new MyDaemon();
Thread daemonThread = new Thread(new ThreadStart(daemon.Start));
daemonThread.Start();
m_daemonUp.WaitOne();
}
else {
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
代码项目站点有一篇很棒的文章,展示了如何在Visual Studio调试器中运行Windows服务,不需要控制台应用程序。
Or 要么
C:\> MyWindowsService.exe /?
MyWindowsService.exe /console
MyWindowsService.exe -console
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.