该服务未响应控制功能(错误2186)

Luc*_*uca 8 c# windows service

我正在Windows平台上使用.NET开发服务.

它一直工作到昨天......但今天它不想开始!这看起来很奇怪,我觉得我错过了一些东西......

我也尝试将源恢复到最后一个工作版本,但没有其他任何事情发生:净启动输出:

该服务未响应控制功能.

什么可能导致这种故障?


可能大多数人想要了解更多.那么,让我给你看一些代码:

服务代码:

#if DEBUG
class iGeckoService : DebuggableService
#else
class iGeckoService : ServiceBase
#endif
{
    static void Main()
    {
#if DEBUG
        if (Debugger.IsAttached == true) {
            DebuggableService[] services = Services;

            // Create console
            AllocConsole();

            // Emulate ServiceBase.Run
            foreach (DebuggableService service in services)
                service.Start(null);

            // Wait for new line
            Console.WriteLine("Press ENTER to exit..."); Console.ReadLine();

            // Emulate ServiceBase.Run
            foreach (DebuggableService service in services)
                service.Stop();
        } else
            ServiceBase.Run(Services);
#else
        ServiceBase.Run(Services);
#endif
    }

#if DEBUG

    static DebuggableService[] Services
    {
        get {
            return (new DebuggableService[] { new iGeckoService() });
        }
    }

    [DllImport("kernel32")]
    static extern bool AllocConsole();

#else

    static DebuggableService[] Services
    {
        get {
            return (new ServiceBase[] { new iGeckoService() });
        }
    }

#endif

    #endregion

    #region Constructors

    /// <summary>
    /// Default constructor.
    /// </summary>
    public iGeckoService()
    {
        // Base properties
        ServiceName = DefaultServiceName;

        // Service feature - Power events
    }

    #endregion

    protected override void OnStart(string[] args)
    {
        try {
            ...

        } catch (Exception e) {
            sLog.Error("Unable to initialize the service. Request to stop.", e);
        }
    }

    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
            {
                     ...
            }
  }

  [RunInstaller(true)]
public class iGeckoDaemonInstaller : Installer
{
    /// <summary>
    /// Default constructor.
    /// </summary>
    public iGeckoDaemonInstaller()
    {
        ServiceProcessInstaller spi = new ServiceProcessInstaller();
        spi.Account = ServiceAccount.LocalSystem;

        ServiceInstaller si = new ServiceInstaller();
        si.ServiceName = iGeckoService.DefaultServiceName;
        si.StartType = ServiceStartMode.Automatic;

        Installers.AddRange(new Installer[] {spi, si});
    }
}

class DebuggableService : ServiceBase
{
    public void Start(string[] args) { OnStart(args); }
}
Run Code Online (Sandbox Code Playgroud)

启动脚本是:

installutil ..\bin\Debug\iGeckoService.exe
net start "Gecko Videowall"
Run Code Online (Sandbox Code Playgroud)

而停止脚本是:

net stop "Gecko Videowall"
installutil /u ..\bin\Debug\iGeckoService.exe
Run Code Online (Sandbox Code Playgroud)

但是,我认为这是一个系统设置,因为应用程序一直运行到最后一天.(叹).


更新

当服务工作时,我使用log4net来记录服务活动(我无法将调试器附加到正在运行的服务......),并且它始终记录.

从现在开始,log4net日志永远不会被创建(即使我启用了内部调试选项),即使我登录Main例程!


另一个更新

似乎应用程序永远不会执行.我减少了每个例程(Main,OnStart,OnStop),并且我运行了一个空服务.OnStart例程在目录上创建一个文件(每个人都可以完全写入),但是当服务启动时,不会创建任何文件.


又一次更新

在Rob的评论刺激下,我在事件查看器上看到了这条消息:

> Faulting application name: iGeckoService.exe, version: 1.0.0.0, time stamp: 0x4c60de6a
> Faulting module name: ntdll.dll, version: 6.1.7600.16385, time stamp: 0x4a5be02b
> Exception code: 0x80000003
> Fault offset: 0x000000000004f190
> Faulting process id: 0x1258
> Faulting application start time: 0x01cb384a726c7167
> Faulting application path: C:\Users\Luca\Documents\Projects\iGeckoSvn\iGeckoService\bin\Debug\iGeckoService.exe
> Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
> Report Id: b096a237-a43d-11df-afc4-001e8c414537
Run Code Online (Sandbox Code Playgroud)

这肯定是服务关闭的原因......没有问题变成:" 如何调试它? "(谢谢Rob,我从来没有想过事件查看器直到现在!)调试它作为控制台应用程序运行它没有显示任何错误,实际上它似乎与服务环境有关.我唯一想到的可能是一些DLL加载失败,因为现在服务是空的......任何想法?

(谢谢大家关注我......我想为你提供比萨饼和啤酒)


解决了!

由于安装和设置MS Application Verifier(x64)导致Main例程之前的崩溃,服务无法启动.卸载该应用程序后,一切正常!

谢谢你们!

Ole*_*leg 5

通常,每个服务都必须遵循以下两个简单的步骤

  • 如果服务经理向他发送了控制代码(例如)SERVICE_CONTROL_STARTSERVICE_CONTROL_STOP以此类推,则应在短时间内返回。使用SetServiceStatus函数服务可以延长此间隔,例如SetServiceStatus通过增加dwCheckPoint值进行调用。(在.NET中,可以使用ServiceBase.RequestAdditionalTime代替)
  • 每个服务都必须SERVICE_CONTROL_INTERROGATE返回代码来回答控制代码。服务管理器使用此控制代码来检测服务是否仍然有效。

如果您的程序不遵循其中一项规则,则会收到错误消息“服务未响应控制功能”。

如果您使用.NET编写程序,则无需直接执行我之前描述的两件事。该ServiceBase班为你在那里做。不过你可以很容易打破这个规则,如果创建一个优先级更高的运行正常线程,或者如果你做一个OnXXX手柄(里面的一些过长的工作OnStopOnStartOnPowerEvent而不调用的等)ServiceBase.RequestAdditionalTime。带有其他线程的其他一些技巧也会引起问题。