I've seen a fair few posts regarding this issue but non that seem to fit my criteria.
Having said that, this is the first one that I've built that "needs" to perform a tremendous amount of logic. All works great for a couple days but after that logic stops working with no log in the event viewer nor firing of an email (exceptions).
I'm wondering if my logic is not correct... Looking for advice and pointers.
public partial class QuayService : ServiceBase
{
private System.Timers.Timer m_mainTimer;
private bool m_timerTaskSuccess;
private Email _email;
public QuayService()
{
InitializeComponent();
_email = new Email();
}
protected override void OnStart(string[] args)
{
try
{
// Create and start a timer.
m_mainTimer = new System.Timers.Timer();
m_mainTimer.Interval = 5000; // every 5 seconds
m_mainTimer.Elapsed += m_mainTimer_Elapsed;
m_mainTimer.AutoReset = false; // makes it fire only once
m_mainTimer.Start(); // Start
m_timerTaskSuccess = false;
_email.SendEmail("Quay", "(Shopify)Quay Service Started",
"(Shopify)Quay Service Successfuly Started");
EventLog.WriteEntry("(Shopify)Quay Service Started...");
}
catch (Exception ex)
{
// Log/Send Email
_email.SendEmail("Quay", "Error starting (Shopify)Quay Service", ex.Message + " " +
ex.InnerException.Message);
EventLog.WriteEntry("Error starting (Shopify)Quay service and timer..." + ex.Message + " " +
ex.InnerException.Message);
}
}
protected override void OnStop()
{
try
{
// Service stopped. Also stop the timer.
m_mainTimer.Stop();
m_mainTimer.Dispose();
m_mainTimer = null;
_email.SendEmail("Quay", "(Shopify)Quay Service stopped",
"(Shopify)Quay Service Successfuly Stopped");
EventLog.WriteEntry("(Shopify)Quay Service stopped...");
}
catch (Exception ex)
{
_email.SendEmail("Quay", "Error stopping (Shopify)Quay Service", ex.Message + " " +
ex.InnerException.Message);
// Log/Send Email
EventLog.WriteEntry("Error stopping (Shopify)Quay timer and service..." + ex.Message + " " +
ex.InnerException.Message);
}
}
void m_mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
var orderUoW = new OrderUoW();
orderUoW.Create();
m_timerTaskSuccess = true;
}
catch (Exception ex)
{
//Error with timer elapsed
m_timerTaskSuccess = false;
_email.SendEmail("Quay", "Error creating (Shopify)Quay order(s)", ex.Message + " " +
ex.InnerException.Message);
EventLog.WriteEntry("Error creating (Shopify)Quay order(Time elapsed event)..." + ex.Message
+ " " + ex.InnerException.Message);
}
finally
{
if (m_timerTaskSuccess)
{
m_mainTimer.Start();
}
}
}
}
To get to this point, I "googled" and "SO'ed" to find the best use of timer... I also have a naïve test layer that reproduces what's in the service, I can run through that fine without any exceptions but this one is driving me nuts.
Any help truly appreciated!
//EDIT
A little explanation on:
var orderUoW = new OrderUoW();
orderUoW.Create();
OrderUoW
is a class in my service that inherits from BatchOrder
which is responsible for many actions including connecting to two databases and polling of the shopify API... OrderUow
is purely to decouple from the business layer but giving access to "x" methods.
All works flawlessly for two days but just seems to halt. I'm not hitting request limits within shopify... so at the moment, I'm at a loss.