服务正在报告 "service has reported an invalid current state 0."

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

请你帮我弄清楚为什么我的服务在事件日志中报告以下错误消息:

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

这是我的主要功能:

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

... 这是我的 UpdateTakerStatus class 在新线程中调用我的逻辑的地方:

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

当我在 visual studio 中调试该服务时,该服务完全正常工作,但是一旦我部署它,它似乎处理项目的速度就非常慢,并报告该错误消息。

然而,它似乎确实在处理项目,这表明代码设置正在执行一些有前途的事情。处理是在循环内我的服务方法调用this.TakerStatusService.CalculateFinalResultsForTakers();中执行的。

我没有包含此代码,因为我担心此错误可能是服务设置不正确的症状 - 而不是线程中执行的代码?

不知是不是我设置有误?

解决方案在这里,我相信:。我遇到了完全相同的问题并找到了这个页面和我链接到的那个页面。在 ServiceStatus 结构的 Interop 声明中将 "long"s 更改为 "uint"s。