gpt4 book ai didi

.net - Windows 服务未启动 : "The service is not responding to the control function."

转载 作者:可可西里 更新时间:2023-11-01 14:14:33 30 4
gpt4 key购买 nike

我一直在浏览 Walkthrough: Creating a Windows Service Application in the Component Designer在 MSDN 上。

我有一些代码并安装了我的服务:

My New Service in the Services MSC

我的代码如下:

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

但是,当我尝试从服务窗口启动服务时,出现以下错误:

enter image description here

如果我尝试使用 net start MyNewService 从控制台启动它,我会收到以下错误:

The service is not responding to the control function.

More help is available by typing NET HELPMSG 2186.

帮助信息类似​​于窗口,即

The service is not responding to the control function.

我该如何解决\调试这个问题?

我正在运行 Windows 8.1 并使用 .NET 4.5.2

最佳答案

要调试问题,您可以执行以下操作:

尝试添加到您的 OnStart 方法(可能是开头):

System.Diagnostics.Debugger.Launch();

解决方案应该在 VS 中打开,当您启动服务时,它应该提示您在 VS 实例中运行调试器。您可以在与您的问题非常相似的问题中找到一些替代解决方案:How to debug the .NET Windows Service OnStart method?

调试愉快!

关于.net - Windows 服务未启动 : "The service is not responding to the control function.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897731/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com