C#自定义Windows服务启动失败:未响应启动控件
我一直在学习一些关于如何在C#中构建Windows服务应用程序的教程。我的服务目标是监视另一个windows服务,如果它停止,则尝试启动它。我相信它应该可以工作,但由于某些原因,服务没有启动,并且出现错误: 错误1503:服务未及时响应启动或控制请求 我希望有人能给我一个正确的方向。欢迎任何指点/指导 这是我的密码:C#自定义Windows服务启动失败:未响应启动控件,c#,windows,service,C#,Windows,Service,我一直在学习一些关于如何在C#中构建Windows服务应用程序的教程。我的服务目标是监视另一个windows服务,如果它停止,则尝试启动它。我相信它应该可以工作,但由于某些原因,服务没有启动,并且出现错误: 错误1503:服务未及时响应启动或控制请求 我希望有人能给我一个正确的方向。欢迎任何指点/指导 这是我的密码: using System; using System.IO; using System.Diagnostics; using System.ServiceProcess; nam
using System;
using System.IO;
using System.Diagnostics;
using System.ServiceProcess;
namespace AEMKeepAlive
{
class AEMKeepAlive : ServiceBase
{
/// <summary>
/// Public Constructor for WindowsService.
/// - Put all of your Initialization code here.
/// </summary>
public AEMKeepAlive()
{
this.ServiceName = "AEM Keep Alive Service";
WriteErrorLog("----------Service started!----------");
// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
this.CanShutdown = true;
this.CanStop = true;
}
/// <summary>
/// Anter messages into a log file
/// </summary>
public static void WriteErrorLog(string msg)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\AEMKeepAlive.log", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + msg);
sw.Flush();
sw.Close();
}
catch
{
}
}
/// <summary>
/// The Main Thread: This is where your Service is Run.
/// </summary>
static void Main()
{
ServiceController myService = new ServiceController();
myService.ServiceName = "CagService";
string svcStatus = myService.Status.ToString();
if (svcStatus == "Running")
{
WriteErrorLog("AEM Service is running! Sleeping for 60 seconds...");
}
else if (svcStatus == "Stopped")
{
WriteErrorLog("WARNING: AEM Service is stopped! Attempting to start the AEM service...");
myService.Start();
string svcStatusWas = "";
while (svcStatus != "Running")
{
if (svcStatus != svcStatusWas)
{
Console.WriteLine("Status: " + svcStatus);
}
svcStatusWas = svcStatus;
myService.Refresh();
svcStatus = myService.Status.ToString();
}
WriteErrorLog("AEM Service has been started! Sleeping for 60 seconds...");
}
else
{
myService.Stop();
WriteErrorLog("Status: " +svcStatus);
while (svcStatus != "Stopped")
{
myService.Refresh();
svcStatus = myService.Status.ToString();
}
WriteErrorLog("WARNING: AEM Service is stopped! Attempting to start the AEM service...");
}
System.Threading.Thread.Sleep(30000);
ServiceBase.Run(new AEMKeepAlive());
}
/// <summary>
/// OnStart(): Put startup code here
/// - Start threads, get inital data, etc.
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
try
{
ServiceBase.Run(new AEMKeepAlive());
base.OnStart(args);
}
catch(Exception e)
{
throw;
}
}
/// <summary>
/// OnStop(): Put your stop code here
/// - Stop threads, set final data, etc.
/// </summary>
protected override void OnStop()
{
WriteErrorLog("----------Service stopped!----------");
base.OnStop();
}
/// <summary>
/// OnShutdown(): Called when the System is shutting down
/// - Put code here when you need special handling
/// of code that deals with a system shutdown, such
/// as saving special data before shutdown.
/// </summary>
protected override void OnShutdown()
{
WriteErrorLog("----------Shutdown initiated----------");
base.OnShutdown();
}
}
}
使用系统;
使用System.IO;
使用系统诊断;
使用System.ServiceProcess;
命名空间保持有效
{
类AEMKeepAlive:ServiceBase
{
///
///WindowsService的公共构造函数。
///-将所有初始化代码放在此处。
///
公共语言
{
this.ServiceName=“AEM保持活动服务”;
WriteErrorLog(“------------服务已启动!------------”);
//这些标志设置是否处理该特定事件
//事件类型。如果需要,请将其设置为true,否则设置为false。
this.CanShutdown=true;
this.CanStop=true;
}
///
///将消息放入日志文件
///
公共静态void WriteErrorLog(字符串msg)
{
StreamWriter sw=null;
尝试
{
sw=新的StreamWriter(AppDomain.CurrentDomain.BaseDirectory+“\\AEMKeepAlive.log”,true);
sw.WriteLine(DateTime.Now.ToString()+“:”+msg);
sw.Flush();
sw.Close();
}
接住
{
}
}
///
///主线程:这是运行服务的地方。
///
静态void Main()
{
ServiceController myService=新ServiceController();
myService.ServiceName=“CagService”;
字符串svcStatus=myService.Status.ToString();
如果(svcStatus==“正在运行”)
{
WriteErrorLog(“AEM服务正在运行!睡眠60秒…”);
}
否则如果(svcStatus==“已停止”)
{
WriteErrorLog(“警告:AEM服务已停止!正在尝试启动AEM服务…”);
myService.Start();
字符串svcStatusWas=“”;
while(svcStatus!=“正在运行”)
{
if(svcStatus!=svcStatusWas)
{
Console.WriteLine(“状态:+svcStatus”);
}
svcStatusWas=svcStatus;
myService.Refresh();
svcStatus=myService.Status.ToString();
}
WriteErrorLog(“AEM服务已启动!睡眠60秒…”);
}
其他的
{
myService.Stop();
WriteErrorLog(“状态:+svcStatus”);
while(svcStatus!=“已停止”)
{
myService.Refresh();
svcStatus=myService.Status.ToString();
}
WriteErrorLog(“警告:AEM服务已停止!正在尝试启动AEM服务…”);
}
系统。线程。线程。睡眠(30000);
Run(新的AEMKeepAlive());
}
///
///OnStart():将启动代码放在此处
///-启动线程、获取初始数据等。
///
///
启动时受保护的覆盖无效(字符串[]args)
{
尝试
{
Run(新的AEMKeepAlive());
base.OnStart(args);
}
捕获(例外e)
{
投
}
}
///
///OnStop():在此处输入停止代码
///-停止线程,设置最终数据等。
///
受保护的覆盖void OnStop()
{
WriteErrorLog(“------------服务已停止!------------”);
base.OnStop();
}
///
///OnShutdown():在系统关闭时调用
///-需要特殊处理时,将代码放在此处
///处理系统关闭的代码,例如
///在关机前保存特殊数据。
///
受保护的覆盖无效OnShutdown()
{
WriteErrorLog(“------------已启动关机----------------”);
base.onShutton();
}
}
}
您的代码不允许OnStart方法终止,因此Windows会认为您的服务未正确完成启动任务,并报告该错误。您需要在单独的线程上启动程序循环,以便OnStart可以退出。我没有在单独的线程上运行服务监视器。修改了我的代码,使OnStart方法为:
/// <summary>
/// OnStart(): Put startup code here
/// - Start threads, get inital data, etc.
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
Library.WriteErrorLog("--------------Service Started--------------");
Thread MyThread = new Thread(new ThreadStart(ServiceMonitor));
MyThread.Name = "Worker Thread";
MyThread.IsBackground = true;
MyThread.Start();
}
服务现在启动,监视器持续运行!:)
public static void ServiceMonitor()
{
for (;;)
{
// Monitor the CagService
ServiceController myService = new ServiceController();
myService.ServiceName = "CagService";
string svcStatus = myService.Status.ToString();
// If the CagService is running, add to the log and sleep for 5 minutes
try
{
if (svcStatus == "Running")
{
Library.WriteErrorLog("AEM Service is running! Sleeping for 5 minutes...");
}
// If the service is stopped, add to the log file and attempt a restart
else if (svcStatus == "Stopped")
{
Library.WriteErrorLog("WARNING: AEM Service is stopped! Attempting to start the AEM service...");
myService.Start();
string svcStatusWas = "";
while (svcStatus != "Running")
{
if (svcStatus != svcStatusWas)
{
Library.WriteErrorLog("Status: " + svcStatus);
}
svcStatusWas = svcStatus;
myService.Refresh();
svcStatus = myService.Status.ToString();
}
Library.WriteErrorLog("AEM Service has been started! Sleeping for 5 minutes...");
}
// If the service has any other status, stop it then restart it.
else
{
myService.Stop();
Library.WriteErrorLog("Status: " + svcStatus);
while (svcStatus != "Stopped")
{
myService.Refresh();
svcStatus = myService.Status.ToString();
}
Library.WriteErrorLog("WARNING: AEM Service is stopped! Attempting to start the AEM service...");
}
}
catch(InvalidOperationException)
{
Library.WriteErrorLog("Invalid Operation Exception! The service cannot be started because it is disabled.");
}
System.Threading.Thread.Sleep(300000);
}
}