Windows 服务未启动:“服务未响应控制功能。”

Ban*_*San 5 .net windows windows-services c#-4.0

我一直在阅读演练:在 MSDN 上的组件设计器中创建 Windows 服务应用程序

我有一些代码并安装了我的服务:

我在服务 MSC 中的新服务

我的代码如下:

namespace WindowsServiceWalkthrough
{
    using System;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Timers;
    using System.Runtime.InteropServices;

    public partial class MyNewService : ServiceBase
    {
        private int _eventId;

        public MyNewService()
        {
            InitializeComponent();
            if (! EventLog.SourceExists("MySource"))
            {
                EventLog.CreateEventSource(
                    "MySource",
                    "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);

        protected override void OnStart(string[] args)
        {
            var serviceStatus = new ServiceStatus
            {
                dwCurrentState = ServiceState.SERVICE_RUNNING,
                dwWaitHint = 100000
            };

            SetServiceStatus(this.ServiceHandle, ref serviceStatus);

            eventLog1.WriteEntry("My Event Log:  In OnStart method", EventLogEntryType.Information);

            var timer = new Timer();
            timer.Interval = 60000; // 60 seconds
            timer.Elapsed += OnTimer;
            timer.Start();

            // Update the service state to Running.
            serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
            SetServiceStatus(ServiceHandle, ref serviceStatus);
        }

        public void OnTimer(object sender, ElapsedEventArgs args)
        {
            // TODO: Insert monitoring activities here.
            eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, _eventId++);
        }

        protected override void OnStop()
        {

        }
    }

    public enum ServiceState
    {
        SERVICE_STOPPED = 0x00000001,
        SERVICE_START_PENDING = 0x00000002,
        SERVICE_STOP_PENDING = 0x00000003,
        SERVICE_RUNNING = 0x00000004,
        SERVICE_CONTINUE_PENDING = 0x00000005,
        SERVICE_PAUSE_PENDING = 0x00000006,
        SERVICE_PAUSED = 0x00000007,
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ServiceStatus
    {
        public long dwServiceType;
        public ServiceState dwCurrentState;
        public long dwControlsAccepted;
        public long dwWin32ExitCode;
        public long dwServiceSpecificExitCode;
        public long dwCheckPoint;
        public long dwWaitHint;
    };
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试从服务窗口启动服务时,出现以下错误:

在此处输入图片说明

如果我尝试从控制台启动它,net start MyNewService我会收到以下错误:

服务未响应控制功能。

通过键入 NET HELPMSG 2186 可以获得更多帮助。

帮助信息则类似于窗口,即

服务未响应控制功能。

我该如何解决\调试这个问题?

我正在运行 Windows 8.1 并使用 .NET 4.5.2

DDa*_*Dan 2

要调试问题,您可以执行以下操作:

尝试添加到您的OnStart方法中(可能是开始):

System.Diagnostics.Debugger.Launch();
Run Code Online (Sandbox Code Playgroud)

该解决方案应该在 VS 中打开,并且当您启动该服务时,它应该提示您在 VS 实例中运行调试器。您可以在与您的问题非常相似的问题中找到一些替代解决方案:How to debug the .NET Windows Service OnStart method?

调试愉快!