Reputation: 1
I'm new to Windows services and looked for a lot of examples and stuff, also searched questions asked before, but I did not yet find a solution for my problem. I'm writing a Windows service in c# that is sending SOAP requests to a server, organises the received data and makes it ready for storing it into the historian.
Initially I made it a console application, but it needs to run scheduled in the background. That's why I made the choice for a Windows service. As a console application the program takes at least 20 minutes, but this can be over an hour, depending on the amount of data.
The Windows service returns an error after like 30 seconds with code 1053: The service did not respond to the start or control request in a timely fashion. I think this has something to do with the service trying to run the whole code in onStart() and the service is thus not returning within a timely fashion.
I'm using the following design:
myProgram.cs:
public MyService()
{
InitializeComponent();
ExecuteProgram();
}
protected override void OnStart(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000 * 60 * 60 *24; // every 24h
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
ExecuteProgram();
}
protected override void OnStop()
{
}
public void ExecuteProgram()
{
//Here is the code for SOAP requests, preparing data and for making the
import file for the historian.
}
In the Program.cs:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
try
{
#if (DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
#else
if (Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
#endif
}
catch (Exception ex)
{
throw (ex);
}
}
}
I hope you can help me with my problem.
Thanks in advance!
Koert
Upvotes: 0
Views: 307