ServiceController似乎无法停止服务

Ant*_*lev 27 windows-services servicecontroller

我正在尝试Topshelf.Host使用以下代码停止本地计算机上的Windows服务(该服务,如果这很重要):

serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?

Ris*_*shi 45

我知道我回答这个问题已经很晚了,但我遇到了类似的问题,即错误:"此时服务无法接受控制消息." 并希望将此作为其他人的参考.

您可以尝试使用powershell来杀死此服务(以管理员身份运行powershell):

#Get the PID of the required service with the help of the service name, say, service name.
$ServicePID = (get-wmiobject win32_service | where { $_.name -eq 'service name'}).processID 

#Now with this PID, you can kill the service
taskkill /f /pid $ServicePID
Run Code Online (Sandbox Code Playgroud)

  • 为我工作。不要忘记也以管理员身份运行 powershell。 (2认同)

小智 13

您的服务正忙于处理某些大型操作或正在转换以更改状态.因此无法接受任何输入...只是认为它比它可以咀嚼更多......

如果您确定没有提供任何大的信息,请转到任务管理器并终止此服务的过程或重新启动您的计算机.

  • 我只是使用任务管理器来终止该服务。然后我可以再次重新启动服务。谢谢。 (2认同)

bla*_*laz 6

我与Topshelf托管服务有完全相同的问题.原因是服务开始时间长,超过20秒.这使得服务处于无法处理进一步请求的状态.只有在从命令行启动服务时我才能重现问题(net start my_service).

具有长星时间的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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jtr*_*ove 2

我也看到过这个问题,特别是当服务开始挂起并且我以编程方式向其发送停止时,该停止成功但不执行任何操作。另外,有时我会看到正在运行的服务的停止命令因相同的异常而失败,但实际上仍然停止该服务。我不认为 API 会按照它所说的去做。这个错误消息解释非常有帮助......

http://technet.microsoft.com/en-us/library/cc962384.aspx