Windows service starts and stops automatically; exception handling problem

I developed a 32-bit service, I run it on Windows 7 Home Premium x64. The problem is when I run it, windows give me the following message

The WLConsumer service on the local computer started and then stopped. Some services stop automatically if they are not used by other services or programs.

In the event log I found the following message

The service cannot be started. System.ArgumentException: The WLConsumer log is already registered as a source on the local computer. in System.Diagnostics.EventLogInternal.CreateEventSource (EventSourceCreationData sourceData) in System.Diagnostics.EventLogInternal.VerifyAndCreateSource (String sourceName, String currentMachineName) in System.Diagnostics, Intent, EventEntryTementry (Event ] rawData) in System.Diagnostics.EventLog.WriteEntry (String message, type EventLogEntryType) in WeblogicConsumerService.WeblogicConsumer.winEventlogMe (String logTxt, String logSrc, Char entryType) in C: \ Program Files (x86) \ CSI \ Logic Weblog cs: line 136 in WeblogicConsumerService.WeblogicConsumer.OnStart (String [] args) in C: \ Program Files (x86) \ CSI \ WeblogicConsumerService \ WeblogicConsumer.cs: line 63 in System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback (object state)

This is my code block in the OnStart () method

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 is the method that I call to log.

  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 } 

When I comment on calls to the winEventLogMe () method in the OnStart () method, the service starts without error. Thus, it is obvious that something is wrong with the winEventLogMe () method. Can someone please help me figure out what the problem is, because I have absolutely no idea how to solve this problem now.

thanx in advance :)


@nick_w I edited my code, as you suggested, the service started successfully. But when stopping, I received the following message:

Failed to stop the service. System.ArgumentException: WLConsumer2012 source is not registered in the ServiceStop log. (It is registered in the SeriviceStartup journal.) "The Source and Log properties must be mapped or you can set Log to an empty string and it will be mapped automatically to the original property. In System.Diagnostics.EventLogInternal.VerifyAndCreateSource (String sourceName, String currentMachineName) in System.Diagnostics.EventLogInternal.WriteEntry (String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte [] rawData) in System.Diagnostics.EventLog.WriteEntry (String message, Type EventLogEntryTypeWebloglogloggerloglog.loglogerloglog.loglogserviceloglog.blog (String logTxt, String logSrc, Char entryType) in C: \ Program Files (x86) \ CSI \ WeblogicConsumerService \ WeblogicConsumer.cs: line 139 in WeblogicConsumerService.WeblogicConsumer.OnStop () in C: \ Program Files (x86) \ CSI \ Weblogicconsu merService \ WeblogicConsumer.cs: line 70 in System.ServiceProcess.ServiceBase.DeferredStop ()

here the OnStop () method is used

  protected override void OnStop() { winEventlogMe("Successful stop", "ServiceStop", 'i'); } 

These event logs scare me. I made the same registration method in other services and have never encountered such problems. How can I get these errors in this service, but it is not much different from all the others that I made :(

+4
source share
2 answers

I think this is your problem:

 EventLog theEvent = new EventLog("WLConsumer"); 

Judging by the exception, I think that WLConsumer is the name of the event source. This means you might be better off with this:

 EventLog theEvent = new EventLog(logSrc); theEvent.Source = "WLConsumer"; 

It is just using parameters differently.

If I do a little decompilation, there is a check like this:

 if (!EventLogInternal.SourceExists(logName, machineName, true)) 

In your case, I would think that this check returns true, which means that it is trying to create a log called WLConsumer , but it does not work because WLConsumer was registered as the event source.

Edit:

When I used the event log in the past, I wrote everything in one combination of source and log. In your case, you seem to use different combinations of source and log every time you write a record.

From MSDN (my attention):

If you are writing to the event log, you must specify or create an event source. You must have administrator rights on the computer to create a new event source. The source logs your application in the event log as a valid source of entries. You can only use the source to write to one log at a time. The source can be any random string, but the name must be different from other sources on the computer. Typically, the source is an application name or other identifying string. An attempt to create a duplicate source value throws an exception. However, a single event log may be associated with multiple sources.

I would suggest the following:

Use WLConsumer (or WLConsumer2012 ) as a source and

  • Define your own log: "WLConsumerServiceEventLog" or something else; or
  • Leave the log blank. They are included in the application log in this case.

Regardless, the standard practice seems to be to do something similar before launching your service for the first time, for example, in the installer (copied directly from the link above):

 // Create the source, if it does not already exist. if(!EventLog.SourceExists("MySource")) { //An event log source should not be created and immediately used. //There is a latency time to enable the source, it should be created //prior to executing the application that uses the source. //Execute this sample a second time to use the new source. EventLog.CreateEventSource("MySource", "MyNewLog"); Console.WriteLine("CreatedEventSource"); Console.WriteLine("Exiting, execute the application a second time to use the source."); // The source is created. Exit the application to allow it to be registered. return; } 

Note the point in the latency of the comments. Magazines are not necessarily created immediately, so he pays for it with this in mind. You can also use EventLogInstaller to create a log. It might be easier if you use the installer to deploy your service.

+4
source

It is extremely important not to overload the on start method and to prevent crashes when starting the service, usually the onstart method starts the main code as a separate thread

0
source

Source: https://habr.com/ru/post/1447846/


All Articles