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 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:
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