Windows services 卸载时自动停止Windows服务
安装服务时,我有一个处理程序,在安装服务后启动该服务Windows services 卸载时自动停止Windows服务,windows-services,servicecontroller,Windows Services,Servicecontroller,安装服务时,我有一个处理程序,在安装服务后启动该服务 private void InitializeComponent() { ... this.VDMServiceInstaller.AfterInstall += ServiceInstaller_AfterInstall; } private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceControl
private void InitializeComponent()
{
...
this.VDMServiceInstaller.AfterInstall += ServiceInstaller_AfterInstall;
}
private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("MyService");
sc.Start();
}
我想在卸载服务之前停止它,因此我向InitializeComponent()添加了一个额外的处理程序
并增加了以下功能:
private void ServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
try
{
ServiceController sc = new ServiceController("MyService");
if (sc.CanStop)
{
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
}
}
catch (Exception exception)
{}
}
但卸载前服务不会停止。我是否未正确使用ServiceController.Stop()函数?下面的内容是否有助于您:
protected override void OnBeforeUninstall(IDictionary savedState)
{
ServiceController controller = new ServiceController("ServiceName");
try
{
if(controller.Status == ServiceControllerStatus.Running | controller.Status == ServiceControllerStatus.Paused)
{
controller.stop();
}
controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0,0,0,15));
controller.Close();
}
catch(Exception ex)
{
EventLog log = new EventLog();
log.WriteEntry("Service failed to stop");
}
finally
{
base.OnBeforeUninstall(savedState);
}
}
这是我试图阻止的窗口: 我已经测试了所有可用的覆盖,在出现提示关闭应用程序的对话框之前,没有一个覆盖被执行 即使是类构造函数也不够早 我的结论是,正如安装程序项目一样,您不能在对话框出现之前通过代码停止服务 因为没有其他方法可以让代码在项目中执行,所以我看不到任何方法可以实现这一点 我真的很希望它是不同的,因为我自己非常需要它,但是在安装程序项目中没有任何“钩子”可用,它进入得足够早以解决问题
我最好的建议是安排两名安装人员 一个充当第二个安装程序的包装器,在安装时会像正常情况一样启动第二个安装程序 但在卸载时,它会先停止服务,然后卸载第二个服务
但这对我来说是一种太多的黑客行为,因此我没有进一步探讨这一点。我想做一些类似的事情,并最终在我的安装程序项目中使用此代码来处理触发BeforeUninstall事件的时间:
private void SessionLoginMonitorInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
try
{
using (ServiceController sv = new ServiceController(SessionLoginMonitorInstaller.ServiceName))
{
if(sv.Status != ServiceControllerStatus.Stopped)
{
sv.Stop();
sv.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
}
catch(Exception ex)
{
EventLog.WriteEntry("Logon Monitor Service", ex.Message, EventLogEntryType.Warning);
}
}
该项目的“自定义操作”部分还有一个用于卸载“我的Windows服务”项目的主输出的操作。这对我很有效,每次我测试它时,它都会给我一个干净的卸载。不起作用。。这几乎就像该函数没有被调用一样,因为该函数还应该引发BeforeUninstall事件。8年前问过,这仍然是一个问题。。在本例中,我创建了用于创建另一个Service2的Service1。Service1只是一个包装器,它有一些API,我可以调用这些API来启动Service2。有趣的是,Service1API将在卸载后激活2分钟以上。我想完全停止服务,然后卸载
private void SessionLoginMonitorInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
try
{
using (ServiceController sv = new ServiceController(SessionLoginMonitorInstaller.ServiceName))
{
if(sv.Status != ServiceControllerStatus.Stopped)
{
sv.Stop();
sv.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
}
catch(Exception ex)
{
EventLog.WriteEntry("Logon Monitor Service", ex.Message, EventLogEntryType.Warning);
}
}