c# – 服务未响应控制功能(错误2186)
我正在
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); } } 启动脚本是: installutil ..binDebugiGeckoService.exe net start "Gecko Videowall" 而停止脚本是: net stop "Gecko Videowall" installutil /u ..binDebugiGeckoService.exe 但是,我认为这是一个系统设置,因为应用程序一直运行到最后一天. (叹). 更新 当服务工作时,我使用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:UsersLucaDocumentsProjectsiGeckoSvniGeckoServicebinDebugiGeckoService.exe > Faulting module path: C:WindowsSYSTEM32ntdll.dll > Report Id: b096a237-a43d-11df-afc4-001e8c414537 这绝对是服务关闭的原因……没有问题变成:“如何调试它?” (谢谢Rob,直到现在我都没想过事件查看器!) (谢谢大家关注我……我想为你提供比萨饼和啤酒) 解决了! 由于安装和设置MS Application Verifier(x64)导致Main例程之前的崩溃,服务无法启动.卸载该应用程序后,一切正常! 谢谢你们! 解决方法
通常,每项服务必须遵循两个简单的事情
>如果服务管理器向他发送控制代码,如SERVICE_CONTROL_START,SERVICE_CONTROL_STOP等,则应在短时间内返回.使用SetServiceStatus函数服务可以延长此间隔,例如调用带有递增的dwCheckPoint值的SetServiceStatus. (在.NET中使用可以使用ServiceBase.RequestAdditionalTime代替) 如果您的程序没有遵循其中一条规则,则会收到错误“服务未响应控制功能”. 如果你用.NET编写一个程序,你不需要直接做我之前描述的两件事. ServiceBase类为您做到了.然而,如果创建一个优先级高于正常运行的线程,或者如果你在OnXXX句柄(OnStop,OnPowerEvent等)内进行了太长时间的工作而不调用ServiceBase.RequestAdditionalTime,则可以轻松破坏此规则.其他一些带有其他线程的技巧也可以制造问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |