我开发了一个32位的服务,并在Windows 7 Home Premium x64上运行它。问题是,当我启动它时,Windows会给我以下消息:
“本地计算机上的WLConsumer服务已经启动,然后停止。某些服务会在其他服务或程序未使用它们时自动停止。”
我在事件日志中找到了以下消息:
“无法启动服务。System.ArgumentException: 日志WLConsumer已在本地计算机上注册为源。 at System.Diagnostics.EventLogInternal.CreateEventSource(EventSourceCreationData sourceData) at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) at WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 136 at WeblogicConsumerService.WeblogicConsumer.OnStart(String[] args) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 63 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)”
这是OnStart()方法中的代码块:
当我在OnStart()方法中注释掉winEventLogMe()方法的调用时,服务可以正常启动,所以显然winEventLogMe()方法存在问题。能否有人帮我找出问题所在,因为我现在完全不知道如何解决这个问题。
提前感谢:)
@nick_w 我已按照您的建议编辑了我的代码,服务成功启动。但在停止时,我收到以下消息:
“无法停止服务。System.ArgumentException: The source 'WLConsumer2012' is not registered in log 'ServiceStop'. (It is registered in log 'SeriviceStartup'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property. at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) at WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 139 at WeblogicConsumerService.WeblogicConsumer.OnStop() in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 70 at System.ServiceProcess.ServiceBase.DeferredStop()
这是OnStop()方法:
“本地计算机上的WLConsumer服务已经启动,然后停止。某些服务会在其他服务或程序未使用它们时自动停止。”
我在事件日志中找到了以下消息:
“无法启动服务。System.ArgumentException: 日志WLConsumer已在本地计算机上注册为源。 at System.Diagnostics.EventLogInternal.CreateEventSource(EventSourceCreationData sourceData) at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) at WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 136 at WeblogicConsumerService.WeblogicConsumer.OnStart(String[] args) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 63 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)”
这是OnStart()方法中的代码块:
protected override void OnStart(string[] args)
{
#region WEBLOGIC CREDENTIALS
try
{
//Weblogic URL
this.url = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("URL").ToString();
//Queue name
this.qName = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("Queue").ToString();
//Weblogic login name
this.user = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("User").ToString();
//Weblogic password
this.pwd = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("Pwd").ToString();
//Weblogic Connection Factory
this.cfName = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("ConnectionFactory").ToString();
//root folder
this.rFolder = Registry.LocalMachine.OpenSubKey(@"Software\CSI_WL").GetValue("root").ToString();
}
catch (Exception e)
{
winEventlogMe(e.Message, "WLRegistryKeys", 'e');
}
#endregion
winEventlogMe("Successful start", "SeriviceStartup", 'i');
synchro.Enabled = true;
}
我调用的方法是winEventLogMe,用于日志记录。
public static void winEventlogMe(string logTxt, string logSrc, char entryType)
{
#region Log
//Log to event log
EventLog theEvent = new EventLog("WLConsumer");
theEvent.Source = logSrc;
if (entryType == 'e')
theEvent.WriteEntry(logTxt, EventLogEntryType.Error);
else if (entryType == 'i')
theEvent.WriteEntry(logTxt, EventLogEntryType.Information);
else if (entryType == 'w')
theEvent.WriteEntry(logTxt, EventLogEntryType.Warning);
else
theEvent.WriteEntry(logTxt, EventLogEntryType.Error);*/
#endregion
}
当我在OnStart()方法中注释掉winEventLogMe()方法的调用时,服务可以正常启动,所以显然winEventLogMe()方法存在问题。能否有人帮我找出问题所在,因为我现在完全不知道如何解决这个问题。
提前感谢:)
@nick_w 我已按照您的建议编辑了我的代码,服务成功启动。但在停止时,我收到以下消息:
“无法停止服务。System.ArgumentException: The source 'WLConsumer2012' is not registered in log 'ServiceStop'. (It is registered in log 'SeriviceStartup'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property. at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) at WeblogicConsumerService.WeblogicConsumer.winEventlogMe(String logTxt, String logSrc, Char entryType) in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 139 at WeblogicConsumerService.WeblogicConsumer.OnStop() in C:\Program Files (x86)\CSI\WeblogicConsumerService\WeblogicConsumer.cs:line 70 at System.ServiceProcess.ServiceBase.DeferredStop()
这是OnStop()方法:
protected override void OnStop()
{
winEventlogMe("Successful stop", "ServiceStop", 'i');
}
这些事件日志开始让我很困惑。我在其他服务中做了相同的记录方法,从来没有遇到过这样的问题。为什么我会在这个服务中出现这些错误,但它与我以前做过的所有其他服务并没有太大区别 :(