I am trying to convert my console application into windows service using .net4.8. I used the topshelf to create a service. The frequency of my service will be every minute. I don't know if I am doing it correctly to call the method within the constructor APIService. Here's my code:
APIService.cs
public APIService()
{
// code here for login in the application
// calling the method class
SAPAPI.PostRequest(uri);
SLMAPI.PostRequest(uri);
_timer = new System.Timers.Timer(60) { AutoReset = true };
_timer.Elapsed += TimerElapsed;
}
private void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string[] lines = new string[] { DateTime.Now.ToString() };
File.AppendAllLines(@"C:\ProgramData\temp\Timer.txt", lines);
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
Program.cs
static void Main(string[] args)
{
var exitCode = HostFactory.Run(x =>
{
x.Service<APIService>(a =>
{
a.ConstructUsing(apiservice => new APIService());
a.WhenStarted(apiservice => apiservice .Start());
a.WhenStopped(apiservice => apiservice .Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("ClientWindowService");
x.SetDisplayName("Client Window Service");
});
int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
Environment.ExitCode = exitCodeValue;
}
Have I done it correctly? Does it automatically trigger the service every minute based on my code? Thank you in advance for your help.