I am currently working on a Windows Service as a non-GUI part of our application.
I wrote a Service which, in Debug conditions, perfectly works. This means I debug by having my static void Main() as following:
#if(!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new TRAservice()
};
ServiceBase.Run(ServicesToRun);
#else
TRAservice Service = new TRAservice();
Service.CanHandlePowerEvent = false;
Service.CanHandleSessionChangeEvent = false;
Service.CanPauseAndContinue = true;
Service.CanShutdown = true;
Service.CanStop = true;
Service.ServiceName = "TRAservice";
Service.AutoLog = false;
Service.SetMethod();
Service.TRAmethod();
#endif
To do some implementation testing, I added a ProjectInstaller and I install my service via the VStools cmd prompt, installutil servicename.exe. (For reference, I include the constructor of the service with the OnStart in the bottom of this post.)
However, when I then try to START the service, I get the following error:
In my event viewer I get 4 messages (2 information (Windows error reports), 2 errors):
I have tried a lot of things, including, but not limited to, removing the debug lines out of the constructor (as elsewhere suggested), rebooting, installing from Release (instead of Debug)...
I will take any suggestion to fix this problem. Thanks in advance.
public TRAservice()
{
InitializeComponent();
CanHandlePowerEvent = false;
CanHandleSessionChangeEvent = false;
CanPauseAndContinue = true;
CanShutdown = true;
CanStop = true;
ServiceName = "TRAservice";
AutoLog = false;
sSource = "TRAservice";
sLog = "TRAlog";
if (!EventLog.SourceExists(sSource))
{
EventLog.CreateEventSource(sSource, sLog);
}
eventLog1 = new System.Diagnostics.EventLog();
eventLog1.Source = sSource;
eventLog1.Log = sLog;
eventLog1.WriteEntry("Service started", EventLogEntryType.Information, 1, 100);
scheduleTimer = new Timer();
scheduleTimer.Interval = 10;
scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);;
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Service started", EventLogEntryType.Information, 1, 100);
flag = true;
sw = true;
lastRun = DateTime.Now;
scheduleTimer.Start();
}
System.ArgumentException: Source TRAservice already exists on the local computer.
However I remove it from the register, this error keeps popping up. Any suggestions? – Shishdem