“启动后又停止”当你的服务器抛出启动过程中的异常通常出现的消息。这可能有很多原因,包括无效路径和由于缺少源代码或权限不足而无法写入应用程序事件日志。
我通常包括运行我的服务作为控制台应用程序的选项。 这使我可以使用Console.WriteLine
来显示任何异常。
以下假定您的服务从System.ServiceProcess.ServiceBase
延伸。
partial class MyService : ServiceBase
{
private static void Main(string[] args)
{
MyService svc = new MyService();
if (Environment.UserInteractive)
RunConsole(args, svc);
else
Run(svc);
}
public MyService()
{
InitializeComponent();
}
protected static bool KeepRunning { get; set; }
protected override void OnStart(string[] args)
{
StartServiceHost();
}
protected override void OnStop()
{
StopServiceHost();
}
protected override void OnShutdown()
{
StopServiceHost();
base.OnShutdown();
}
private static void RunConsole(string[] args, ConverterService svc)
{
// need to hold on to Ctrl+C, otherwise StopServiceHost() never gets called
Console.CancelKeyPress += (sender, e) => ShutDown(svc);
KeepRunning = true;
svc.OnStart(args);
Console.WriteLine("Press <Ctrl+C> to exit.");
while (KeepRunning)
{
Console.ReadLine();
}
}
private void StartServiceHost()
{
// start your service
}
private void StopServiceHost()
{
// stop your service
}
private static void ShutDown(MyService svc)
{
Console.WriteLine("exiting...");
svc.OnStop();
KeepRunning = false;
}
}
请发布您的'OnStart'方法的代码。 –
我已将随机链接移除,看起来像是您的网站。不要这样做。完全无关你的帖子。 – tnw
Windows服务常见的首次部署问题与事件日志有关。如果您的服务使用自定义源写入应用程序事件日志,则需要创建源代码。通常安装人员会为你做这个。暂时尝试在LocalSystem等特权帐户下运行服务,以查看您的事件是否显示。 –