Apologies for my English, I'm not a native speaker.
I'm trying to make a Windows service. If I try to build, install and run a VS template I don't get any errors.
I have ported my winform application to a service, made an installer, added some data sources, added a reference for the webservice, added some classes, but DIDN'T add any code to OnStart() and OnStop(). My code builds correctly and I can start and stop the service from the service manager.
However, if I add some code to the service class (which I don't call anywhere) and if I don't add code to OnStart() and to OnStop() then I can't start the service and the error is something like "The service doesn't respond the control functions". In the event log I can see the exception:
System.ArgumentOutOfRangeException
Stack:
in System.String.InternalSubStringWithChecks(Int32, Int32, Boolean)
in System.String.Substring(Int32, Int32)
in UpdaterFIAS.FIASMainClass.getNameFile(System.String, System.String, System.String)
in UpdaterFIAS.FIASMainClass..ctor()
in UpdaterFIAS.Updater..ctor()
in UpdaterFIAS.Program.Main()
And I can see here my function getNameFile()
is throwing an Exception. However, this isn't called in my code because I have empty OnStart(). So, how can I find what went wrong if the event log doesn't write anything ( if it is in the OnStart() ) ? And I can't attach a debugger to it because it throws this exception.
edit: Forgot to say, my code works correctly when I use windows forms but here I don't call anything in OnStart, the project builds without errors but I have an exception when starting the service.
EDIT 2: Program.cs code:
namespace UpdaterFIAS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Updater()
};
ServiceBase.Run(ServicesToRun);
}
}
}
Updater.cs code:
namespace UpdaterFIAS
{
public partial class Updater : ServiceBase
{
public Updater()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
FIASMainClass mainFIAS = new FIASMainClass();
protected override void OnStart(string[] args)
{
//timer1 = new System.Timers.Timer(5000);
//timer1.Elapsed += timer1_Elapsed;
//timer1.AutoReset = false;
//timer1.Enabled = true;
//ServiceStarterThread = new Thread(ServiceStarter);
//ServiceStarterThread.Start();
eventLog1.WriteEntry("In OnStart");
//mainFIAS.Start();
}
protected override void OnStop()
{
//if (updater != null && (updater.ThreadState != System.Threading.ThreadState.Aborted && updater.ThreadState != System.Threading.ThreadState.Stopped)) updater.Abort();
//if (log != null && (log.ThreadState != System.Threading.ThreadState.Aborted && log.ThreadState != System.Threading.ThreadState.Stopped)) log.Abort();
//log.Abort();
//timer1.Enabled = false;
//timer1.Dispose();
eventLog1.WriteEntry("In OnStop");
//mainFIAS.Stop();
}
}
}
EDIT 3: FIASMainClass.cs code:
namespace UpdaterFIAS
{
class FIASMainClass
{
public FIASMainClass()
{ }
public void Start()
{
ServiceStarterThread = new Thread(ServiceStarter);
ServiceStarterThread.Start();
}
public void Stop()
{
if (updater != null && (updater.ThreadState != System.Threading.ThreadState.Aborted && updater.ThreadState != System.Threading.ThreadState.Stopped)) updater.Abort();
if (log != null && (log.ThreadState != System.Threading.ThreadState.Aborted && log.ThreadState != System.Threading.ThreadState.Stopped)) log.Abort();
}
private void ServiceStarter()
{
...
}
...
...
...
}
}