Windows services ServiceController似乎无法停止服务
我正试图用以下代码停止本地计算机上的Windows服务(该服务是Windows services ServiceController似乎无法停止服务,windows-services,servicecontroller,Windows Services,Servicecontroller,我正试图用以下代码停止本地计算机上的Windows服务(该服务是Topshelf.Host,如果有必要): serviceController.Stop(); serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout); timeout设置为1小时,但服务从未真正停止。奇怪的是,在服务MMC管理单元中,我首先看到它处于“停止”状态,但过了一段时间,它又恢复为“已启动”。但是,当我尝试手动停止时,会发生错误:
Topshelf.Host
,如果有必要):
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
timeout
设置为1小时,但服务从未真正停止。奇怪的是,在服务MMC管理单元中,我首先看到它处于“停止”状态,但过了一段时间,它又恢复为“已启动”。但是,当我尝试手动停止时,会发生错误:
Windows could not stop the Topshelf.Host service on Local Computer.
Error 1061: The service cannot accept control messages at this time.
我在这里遗漏了什么吗?我也看到了这个问题,特别是当服务启动挂起时,我以编程方式向它发送一个stop,它成功了,但什么也不做。有时,我看到运行中的服务的stop命令失败,出现同样的异常,但实际上仍然停止了服务。我不认为API可以做到它所说的。此错误消息解释非常有用
在将代码从旧的多分区框移动到新的单分区框时,我刚刚解决了这个问题。在服务站,我给D写了封信:因为它已经不存在了,我收到了一个1061错误。但是,除非您使用回调委托将调用转移到另一个线程,否则在联机期间的任何长时间操作都会导致这种情况。您的服务正忙于处理某个大操作,或者正在转换以更改状态。因此,它无法接受更多的输入…只需将其视为摄取了超出其咀嚼能力的东西
如果您确定没有向它提供任何重要信息,只需转到任务管理器并终止此服务的进程或重新启动您的计算机。我遇到了类似的问题。此错误有时会发生,因为服务无法再接受控制消息,这可能是由于存在特定服务日志文件的服务器中的磁盘空间问题造成的。 如果发生这种情况,你也可以考虑下面的选项。
我知道我回答这个问题已经很晚了,但我遇到了一个类似的问题,即错误:“服务此时无法接受控制消息。”,并希望添加此内容作为其他人的参考 您可以尝试使用powershell终止此服务(以管理员身份运行powershell):
我对Topshelf托管服务也有同样的问题。原因是服务启动时间过长,超过20秒。这使服务处于无法处理进一步请求的状态。 只有在从命令行启动服务时(净启动我的_服务),我才能重现这个问题 长启动时间Topshelf服务的正确初始化如下:
namespace Example.My.Service
{
using System;
using System.Threading.Tasks;
using Topshelf;
internal class Program
{
public static void Main()
{
HostFactory.Run(
x =>
{
x.Service<MyService>(
s =>
{
MyService testServerService = null;
s.ConstructUsing(name => testServerService = new MyService());
s.WhenStarted(service => service.Start());
s.WhenStopped(service => service.Stop());
s.AfterStartingService(
context =>
{
if (testServerService == null)
{
throw new InvalidOperationException("Service not created yet.");
}
testServerService.AfterStart(context);
});
});
x.SetServiceName("my_service");
});
}
}
public sealed class MyService
{
private Task starting;
public void Start()
{
this.starting = Task.Run(() => InitializeService());
}
private void InitializeService()
{
// TODO: Provide service initialization code.
}
[CLSCompliant(false)]
public void AfterStart(HostControl hostStartedContext)
{
if (hostStartedContext == null)
{
throw new ArgumentNullException(nameof(hostStartedContext));
}
if (this.starting == null)
{
throw new InvalidOperationException("Service start was not initiated.");
}
while (!this.starting.Wait(TimeSpan.FromSeconds(7)))
{
hostStartedContext.RequestAdditionalTime(TimeSpan.FromSeconds(10));
}
}
public void Stop()
{
// TODO: Provide service shutdown code.
}
}
}
名称空间示例.My.Service
{
使用制度;
使用System.Threading.Tasks;
使用Topshelf;
内部课程计划
{
公共静态void Main()
{
霍斯特工厂,快跑(
x=>
{
x、 服务(
s=>
{
MyService testServerService=null;
s、 ConstructUsing(name=>testServerService=newmyservice());
s、 开始时(service=>service.Start());
s、 停止时(service=>service.Stop());
s、 启动后服务(
上下文=>
{
if(testServerService==null)
{
抛出新的InvalidOperationException(“尚未创建服务”);
}
testServerService.AfterStart(上下文);
});
});
x、 SetServiceName(“我的服务”);
});
}
}
公共密封类MyService
{
私人任务启动;
公开作废开始()
{
this.starting=Task.Run(()=>InitializeService());
}
私有void InitializeService()
{
//TODO:提供服务初始化代码。
}
[CLSCompliant(false)]
public void AfterStart(HostControl hostStartedContext)
{
if(hostStartedContext==null)
{
抛出新ArgumentNullException(nameof(hostStartedContext));
}
if(this.starting==null)
{
抛出新的InvalidOperationException(“服务启动未启动”);
}
同时(!this.starting.Wait(TimeSpan.FromSeconds(7)))
{
hostStartedContext.RequestAdditionalTime(TimeSpan.FromSeconds(10));
}
}
公共停车场()
{
//TODO:提供服务关闭代码。
}
}
}
我知道它是在不久前打开的,但我有点缺少Windows命令提示符的选项,所以只是为了完整起见
- 最终,您可以缩小执行文件的范围,即Image name=notepad.exe
- 示例:TASKKILL/F/PID 111;TASKKILL/F/IM notepad.exe
我遇到了一个类似的问题,发现这是由于其中一个服务陷入启动挂起、停止挂起或停止的状态。 重新启动服务器或尝试重新启动服务无效。 为了解决这个问题,我在服务器上运行了任务管理器,并在“详细信息”选项卡中找到了通过结束任务而卡住并终止进程的服务。结束任务后,我能够毫无问题地重新启动服务 简言之: 1.转到任务管理器 2.单击“详细信息”选项卡 3.找到你的服务
namespace Example.My.Service
{
using System;
using System.Threading.Tasks;
using Topshelf;
internal class Program
{
public static void Main()
{
HostFactory.Run(
x =>
{
x.Service<MyService>(
s =>
{
MyService testServerService = null;
s.ConstructUsing(name => testServerService = new MyService());
s.WhenStarted(service => service.Start());
s.WhenStopped(service => service.Stop());
s.AfterStartingService(
context =>
{
if (testServerService == null)
{
throw new InvalidOperationException("Service not created yet.");
}
testServerService.AfterStart(context);
});
});
x.SetServiceName("my_service");
});
}
}
public sealed class MyService
{
private Task starting;
public void Start()
{
this.starting = Task.Run(() => InitializeService());
}
private void InitializeService()
{
// TODO: Provide service initialization code.
}
[CLSCompliant(false)]
public void AfterStart(HostControl hostStartedContext)
{
if (hostStartedContext == null)
{
throw new ArgumentNullException(nameof(hostStartedContext));
}
if (this.starting == null)
{
throw new InvalidOperationException("Service start was not initiated.");
}
while (!this.starting.Wait(TimeSpan.FromSeconds(7)))
{
hostStartedContext.RequestAdditionalTime(TimeSpan.FromSeconds(10));
}
}
public void Stop()
{
// TODO: Provide service shutdown code.
}
}
}