如何在项目构建中检测Windows服务

时间:2022-09-11 16:30:41

I'm working on a solution that contains a Windows Service and a WinForms client that interacts with that service.

我正在开发一个包含Windows服务和与该服务交互的WinForms客户端的解决方案。

In my pre-build and post-build events, I have some net start and net stop commands to start and stop the service, but there are times when this causes a problem (file's not found, service is already stopped, etc.).

在我的预构建和后构建事件中,我有一些net start和net stop命令来启动和停止服务,但有时会导致问题(找不到文件,服务已经停止等)。

Is there a way to test if a service is running or installed prior to issuing net start?

有没有办法在发布净启动之前测试服务是否正在运行或安装?

I'd like to put this test in .cmd file and run it in the pre-build event for the project.

我想将此测试放在.cmd文件中,并在项目的预构建事件中运行它。

2 个解决方案

#1


You actually don't need to install, start and stop service every time. Instead, consider adding a command-line key to your service executable so that when it's specified, is runs as a service (that is, performs usual ServiceBase.Run() stuff), and when this key is absent it runs as a regular console application. You'll get an added benefit of being able to dump logger output directly to the console so that debugging will be a whole lot easier.

实际上,您每次都不需要安装,启动和停止服务。相反,请考虑向服务可执行文件添加命令行密钥,以便在指定时将其作为服务运行(即执行常用的ServiceBase.Run()内容),并且当此密钥不存在时,它将作为常规控制台运行应用。您可以获得额外的好处,即能够将记录器输出直接转储到控制台,以便调试变得更加容易。

if(args.GetLength(0) == 1 && args[0].ToUpper() == "/SERVICE")
{
    ServiceBase[] services = new ServiceBase[] { new MyService() };
    ServiceBase.Run(services);        
} // if
else
{
    InitAndStartWhateverIsNecessaryToRunServer();
    Console.ReadLine();    
} // else

#2


Stick this into a vb script file and add to the pre and post build events.

将其粘贴到vb脚本文件中并添加到构建前和事件后的事件中。

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
             ("SELECT * FROM Win32_Service WHERE Name = 'someService'") 
Set objService = colRunningServices(0) 
If objService.State <> "Running" And objService.State <> "Starting" Then
  objService.StartService()
End If 

#1


You actually don't need to install, start and stop service every time. Instead, consider adding a command-line key to your service executable so that when it's specified, is runs as a service (that is, performs usual ServiceBase.Run() stuff), and when this key is absent it runs as a regular console application. You'll get an added benefit of being able to dump logger output directly to the console so that debugging will be a whole lot easier.

实际上,您每次都不需要安装,启动和停止服务。相反,请考虑向服务可执行文件添加命令行密钥,以便在指定时将其作为服务运行(即执行常用的ServiceBase.Run()内容),并且当此密钥不存在时,它将作为常规控制台运行应用。您可以获得额外的好处,即能够将记录器输出直接转储到控制台,以便调试变得更加容易。

if(args.GetLength(0) == 1 && args[0].ToUpper() == "/SERVICE")
{
    ServiceBase[] services = new ServiceBase[] { new MyService() };
    ServiceBase.Run(services);        
} // if
else
{
    InitAndStartWhateverIsNecessaryToRunServer();
    Console.ReadLine();    
} // else

#2


Stick this into a vb script file and add to the pre and post build events.

将其粘贴到vb脚本文件中并添加到构建前和事件后的事件中。

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
             ("SELECT * FROM Win32_Service WHERE Name = 'someService'") 
Set objService = colRunningServices(0) 
If objService.State <> "Running" And objService.State <> "Starting" Then
  objService.StartService()
End If