Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service not starting: "The service is not responding to the control function."

I've been going through the Walkthrough: Creating a Windows Service Application in the Component Designer on MSDN.

I have some code and my service is installed:

My New Service in the Services MSC

My code is as below:

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;
    };
}

However, when I try to start the service from the services window I get the following error:

enter image description here

If I try to start it from the console with net start MyNewService I get the following error:

The service is not responding to the control function.

More help is available by typing NET HELPMSG 2186.

The help message is then the similar to the window, i.e.

The service is not responding to the control function.

How can I fix\debug this?

I'm running Windows 8.1 and using .NET 4.5.2

like image 630
BanksySan Avatar asked Oct 19 '22 05:10

BanksySan


2 Answers

For debugging the problem, you could do the following:

Try to add to your OnStart method (possibly beginning):

System.Diagnostics.Debugger.Launch();

The solution should be open in VS, and when you start the service, it should prompt you to run your debugger in your VS instance. You can find some alternative solutions in a very similar problem than yours: How to debug the .NET Windows Service OnStart method?

Happy debugging!

like image 95
DDan Avatar answered Nov 15 '22 06:11

DDan


Thanks to DDan's advice on attaching the debugger, I was able to solve my issue.

My problem was that I was trying to monitor a folder in the program files directory for which the service did not have permission and I was getting the error The directory name [path of folder] is invalid... I changed the directory to C:\temp and the service worked fine.

I would have left this as a comment but don't currently have the required reputation. Hopefully this will help someone.

like image 39
Lewis Hamill Avatar answered Nov 15 '22 07:11

Lewis Hamill