Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service is reporting "service has reported an invalid current state 0."

Please could you help me to work out why my service is reporting in the event log the following error message:

The [service name] service had reported an invalid current state 0.

Here's my main function:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        // Setup Ninject
        var kernel = ConfigureNinject();

        #if DEBUG

        var service = kernel.Get<UpdateTakerStatus>();
        service.onDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

        #else

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            kernel.Get<UpdateTakerStatus>()
        };
        ServiceBase.Run(ServicesToRun);

        #endif
    }
}

... and here is my UpdateTakerStatus class where my logic is called in a new thread:

public partial class UpdateTakerStatus : ServiceBase
{
    private Thread WorkerThread;
    private AutoResetEvent StopRequest = new AutoResetEvent(false);
    private ServiceStatus serviceStatus = new ServiceStatus();

    public ITakerStatusService TakerStatusService { get; set; }

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

    public UpdateTakerStatus(ITakerStatusService takerStatusService)
    {
        InitializeComponent();

        this.TakerStatusService = takerStatusService;
    }

    public void onDebug()
    {
        this.OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        // Update the service state to Start Pending.
        serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
        serviceStatus.dwWaitHint = 100000;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);

        this.WorkerThread = new Thread(UpdateTakers);
        this.WorkerThread.Start();

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

    private void UpdateTakers()
    {
        while (true)
        {
            #if (!DEBUG)
            // Run this code until the service is stopped
            if (StopRequest.WaitOne())
            {
                return;
            }
            #endif

            this.TakerStatusService.CalculateFinalResultsForTakers();
        }
    }

    protected override void OnStop()
    {
        serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);

        this.StopRequest.Set();
        this.WorkerThread.Join();

        serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);
    }
}

The service works completely fine when I debug it in visual studio, but as soon as I deploy it it appears to process the items quite slowly and reports that error message.

It does however appear to be processing items which indicates that the code setup is executing something which is promising. The processing is performed in my service method call this.TakerStatusService.CalculateFinalResultsForTakers(); within the loop.

I haven't included the code for this as I'm concerned that this error might be symptomatic of the service generally being set up incorrectly - not the code executed in the thread?

I wonder if I have something set up wrong?

like image 575
Luke Avatar asked Sep 28 '22 16:09

Luke


1 Answers

Solution is here, I believe: Service Causes SCM Error "reported an invalid current state 0". I hit exactly the same problem and found this page and the one I've linked to. Change your "long"s to "uint"s in your Interop declaration of the ServiceStatus struct.

like image 66
Rich Avatar answered Oct 12 '22 23:10

Rich