Windows service does not start: "Service does not respond to management function."

I went through the Walkthrough: Creating a Windows Service Application in Component Designer on MSDN.

I have a code and my service is installed:

My new service in MSC Services

My code is as follows:

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 using net start MyNewService , I get the following error:

The service does not respond to the management function.

For further assistance, call NET HELPMSG 2186.

The help message then looks like a window, i.e.

The service does not respond to the management function.

How can I fix \ debug this?

I am running Windows 8.1 and using .NET 4.5.2

+4
source share
2 answers

To debug the problem, you can do the following:

Try adding OnStart to your method (maybe OnStart started):

 System.Diagnostics.Debugger.Launch(); 

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

Happy debugging!

+2
source

Thanks to DDan's recommendations on attaching a debugger, I was able to solve my problem.

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

I would leave this as a comment, but currently does not have the necessary reputation. Hope this helps someone.

0
source

All Articles