有 5 个 Windows 服务使用相同的 exe。这些在 SCM 中设置为自动。当服务器重新启动时,这些 Windows 服务会抛出错误“Windows 服务无法及时启动”。问题在于代码的编写方式。为了确定问题出在哪里,我在代码中编写了 EventView 日志记录,但看到每次 SCM 尝试启动服务时都会调用主入口点,即 static void main。下面是代码
static class Program { /// /// 应用程序的主入口点。/// /// 带有事件记录的原始代码 static void Main() { GetAllServicesToRun(); }
private static void GetAllServicesToRun()
{
EventLog ev = new EventLog("");
try
{
string msg = string.Empty;
ev.Source = "STARTAPPLN";
msg = "Appln start" + " at time: " + DateTime.Now.ToString("hh:mm:ss:fff");
ev.WriteEntry(msg, EventLogEntryType.FailureAudit);
TaskServiceConfig tsc = (TaskServiceConfig)System.Configuration.ConfigurationManager.GetSection("TaskServiceConfigSection");
IList<ServiceBase> ServicesToRun = new List<ServiceBase>();
int i = 0;
foreach (var config in tsc.TaskServices)
{
Hashtable arguments = new Hashtable();
arguments.Add("taskServiceName", ((TaskServices.ConfigSection.TaskServiceConfig.TaskServiceElement)config).Value);
ev.Source = "ForLoop_" + i.ToString() + "_START";
msg = "start For Loop_" + i.ToString() + " at time: " + DateTime.Now.ToString("hh:mm:ss:fff") + " :SN: " + arguments["taskServiceName"].ToString();
ev.WriteEntry(msg, EventLogEntryType.FailureAudit);
ServicesToRun.Add(new Service1(arguments));
ev.Source = "ForLoop_" + i.ToString() + "_END";
msg = "end For Loop_" + i.ToString() + " at time: " + DateTime.Now.ToString("hh:mm:ss:fff");
ev.WriteEntry(msg, EventLogEntryType.FailureAudit);
i = i + 1;
}
ServiceBase.Run(ServicesToRun.ToArray());
}
由于主入口点被多次调用,SCM 将服务标记为超时,因为初始启动时间超过 30 秒。我无法更改注册表设置以增加启动时间。我怀疑在这种情况下不应多次调用主入口点。