Issue starting Windows service after moving from 2003 to 2016. DataTables causing problems

Hi all,

New here, so I hope I manage to explain everything in enough detail and accurately. Having some issues with DataTables in a Windows service. I appreciate this is a long post, and might take some reading but it has been driving me crazy all week now as I can’t figure this out

My position is that I have inherited a C# windows service at work and now it needs to be moved from a 2k3 server to a new Windows 2016 server and I am currently having a few problems in getting the service to start.

The purpose of our service is to query my teams database on a regular basis and email out to my team so that we can see how our daily batch is progressing. The service worked completely fine on the 2k3 server. It hasn’t been changed since it was first built in perhaps 2014 where I believe it was compiled against .Net Framework 2.0. The 2016 server is on the 4.6 framework, so the first thing I tried was changing this within the service properties in Visual Studio then building and trying to start on the new server alas this wasn’t successful. What happens when I start the service is that it starts then comes up with an error message saying that it had stopped straight away.

The steps I took after this were to add lines to write to our log file to help find out how far the service got before it stopped. It carries out a few steps, but it seems to get stuck when it starts to use a DataTable.

When the service starts this is what happens in OnStart:

 //****** On Start service ****** protected override void OnStart(string[] args) { CORE = new Core_Code(); //Write to Log CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** Windows Service Started ****"); //Email xmlSettings(); startSetup(); } 

Everything within xmlSettings() runs okay. We start to get issues within startSetup(); If the log lines make sense, *** 30 is fine, *** 31 is fine, but I don’t reach *** 32 so the issue lies within calculatePercentage();

startSetup() contains

private void startSetup() { CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 30 ****"); currentStatus("Completed"); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31 ****"); calculatePercentage(); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 32 ****"); //Check Timer Settings this.checktimer = new System.Timers.Timer(60000); //60Seconds CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 33 ****"); //this.checktimer = new System.Timers.Timer(10000); //10Seconds this.checktimer.AutoReset = true; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 34 ****"); this.checktimer.Elapsed += new System.Timers.ElapsedEventHandler(this.checktimer_Elapsed); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 35 ****"); //Email Timer Settings //this.emailtimer = new System.Timers.Timer(3600000); //1hour //this.emailtimer = new System.Timers.Timer(1800000); //30Min //this.emailtimer = new System.Timers.Timer(30000); //30Seconds this.emailtimer = new System.Timers.Timer(statusTimer); //XML Timed CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 36 ****"); this.emailtimer.AutoReset = true; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 37 ****"); this.emailtimer.Elapsed += new System.Timers.ElapsedEventHandler(this.emailtimer_Elapsed); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 38 ****"); this.emailtimer.Enabled = false; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 39 ****"); //Check if PBE is already running DataTable dt = checkJobSchedule(); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 40 ****"); //Check to see if start of day has started if (loadedItems.Length > 1 || EODInProgress == true) { //SOD already started Started = true; currentStatus("In Progress"); //Setting sent email to false periodEndChargeSent = false; generateChargeDiscountSent = false; OFTOutputSent = false; generateMISSent = false; this.emailtimer.Enabled = true; emailtimer_Elapsed(null, null); } else { //SOD not started this.emailtimer.Enabled = false; } //Check Timer Enabled this.checktimer.Enabled = true; } 

calculatePercentage() contains the following and this is where we see DataTable being used. Following the log lines again, I get to 31.4.2 but I don’t get any further than that. Seems like the issue lies within when we create the DataTable

public void calculatePercentage() { double processed = 0; double yesterdayProcessed = 0; //Processed string sql = "select count(*) as processed_count from mi3pbe.job_schedule where status='P'"; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.4 ****"); object[] titlesql = new object[1]; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.4.1 ****"); titlesql[0] = "PROCESSED_COUNT"; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.4.2 ****"); DataTable dt = CORE.OracleScript(sql, titlesql); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.4.3 ****"); foreach (DataRow row in dt.Rows) { CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.4.4 ****"); processed = Convert.ToInt32(row["PROCESSED_COUNT"]); } //Processed Yesterday CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.5 ****"); string sql2 = "select max(count(*)) as processed_count from mi3pbe.job_schedule_history where status='P' group by post_date"; object[] titlesql2 = new object[1]; titlesql2[0] = "PROCESSED_COUNT"; DataTable dt2 = CORE.OracleScript(sql2, titlesql2); foreach (DataRow row2 in dt2.Rows) { yesterdayProcessed = Convert.ToInt32(row2["PROCESSED_COUNT"]); } //Calculate Percentage CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.6 ****"); batchPercentage = Convert.ToString(Math.Truncate((processed / yesterdayProcessed) * 100)); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.7 ****"); //Save Percentage to XML XmlDocument doc = new XmlDocument(); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.8 ****"); doc.Load("c:\MASTER.config"); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.9 ****"); //Staus XmlNode root1 = doc.DocumentElement["batchPercentage"]; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.10 ****"); root1.FirstChild.InnerText = batchPercentage; CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.11 ****"); doc.Save("c:\MASTER.config"); CORE.appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - **** 31.12 ****"); } 

Has anything changed with DataTables since the service was first created on NET framework 2.0 for a 2k3 server? Any ideas on what I can try to proceed and get the service starting?

Where it says CORE it is calling the following file for things like the database connection and how to write to the log file

using System; using System.Text; using System.Data; using System.Data.OracleClient; using System.Xml; using System.Net; using System.Diagnostics; namespace PBEService { class Core_Code { public string logLocation; public string emailUsername; public string emailPassword; public string emailAddress; //****** Send Email ****** public void sendEmail(string msgTo, string msgSubject, string msgBody) { try { //Get XML Email Settings XmlDocument doc1 = new XmlDocument(); doc1.Load("c:\MASTER.config"); XmlNode rootSet1 = doc1.DocumentElement["emailUsername"]; emailUsername = rootSet1.FirstChild.InnerText; XmlNode rootSet2 = doc1.DocumentElement["emailPassword"]; emailPassword = rootSet2.FirstChild.InnerText; XmlNode rootSet3 = doc1.DocumentElement["emailAddress"]; emailAddress = rootSet3.FirstChild.InnerText; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(msgTo); message.Subject = msgSubject; message.From = new System.Net.Mail.MailAddress(emailAddress); message.Body = msgBody; message.IsBodyHtml = true; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("***EMAIL SERVER***"); smtp.UseDefaultCredentials = false; NetworkCredential basicCredential = new NetworkCredential(emailUsername, emailPassword); smtp.Credentials = basicCredential; smtp.EnableSsl = true; smtp.Send(message); //Log Email Sent appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - '" + msgSubject + "' Email Sent Successfully to '" + msgTo + "'"); } catch { //Log Email Not Sent appLog(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " - '" + msgSubject + "' Email FAILED to send to '" + msgTo + "'"); } } //****** Database ****** public string OracleConnString(string host, string port, string servicename, string user, string pass) { return String.Format( "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})" + "(PORT={1}))(CONNECT_DATA=(SERVICE_NAME={2})));User Id={3};Password={4};", host, port, servicename, user, pass); } //****** Database ****** public DataTable OracleScript(string sql, object[] titlesql) { DataTable dt = new DataTable(); string connectionstring = OracleConnString("***HOST****", "***PORT***", "***SERVICE NAME***", "***USERNAME***", "***PASSWORD***"); using (OracleConnection conn = new OracleConnection(connectionstring)) // connect to oracle { conn.Open(); // open the oracle connection using (OracleCommand comm = new OracleCommand(sql, conn)) // create the oracle sql command { using (OracleDataReader rdr = comm.ExecuteReader()) // execute the oracle sql and start reading it { object[] oArray = new object[Convert.ToInt64(rdr.VisibleFieldCount)]; for (int i = 0; i < titlesql.Length; i++) { dt.Columns.Add(Convert.ToString(titlesql[i]), typeof(string)); } while (rdr.Read()) // loop through each row from oracle { for (int i = 0; i < rdr.VisibleFieldCount; i++) { oArray[i] = rdr[i]; } dt.Rows.Add(oArray); } rdr.Close(); // close the oracle reader } } conn.Close(); // close the oracle connection if (dt.Rows.Count == 0) { dt.Rows.Add(); } return dt; } } //****** Convert Tables to HTML ****** public string getHTML(DataTable dt) { StringBuilder myBuilder = new StringBuilder(); myBuilder.Append("<table border style='font-family:Courier, monospace;font-size:6px;text-align:left;margin:7px;width:600px;border-collapse:collapse;color:#00287a;border-bottom:2px solid #6678b1;border-bottom: 1px solid #ccc;color: #669;padding: 0px 8px;border-top-style:double;border-top-color:blue;border-top-width:thick;'>"); //Table Column myBuilder.Append("<tr>"); foreach (DataColumn myColumn in dt.Columns) { myBuilder.Append("<td bgcolor='0A6EA9'> <FONT COLOR='FFFFFF'><b>"); myBuilder.Append(myColumn.ColumnName); myBuilder.Append("</FONT></b></td>"); } myBuilder.Append("</tr>"); int i = 0; //Table Row foreach (DataRow myRow in dt.Rows) { if (i == 0) { myBuilder.Append("<tr>"); i = 1; } else { myBuilder.Append("<tr bgcolor='D2EDFC'>"); i = 0; } //Sets row to red try { if (dt.TableName == "EOD1") { if (Convert.ToInt32(myRow["NUM_OF_RECORDS"]) > Convert.ToInt32(myRow["MAX_LIMIT"]) || Convert.ToInt32(myRow["NUM_OF_RECORDS"]) < Convert.ToInt32(myRow["MIN_LIMIT"])) { myBuilder.Append("<FONT COLOR='FF0000'><b>"); } } } catch { } foreach (DataColumn myRows in dt.Columns) { myBuilder.Append("<td>"); myBuilder.Append(myRow[myRows.ColumnName].ToString()); myBuilder.Append("</td>"); } myBuilder.Append("</FONT></b></td>"); myBuilder.Append("</tr>"); } //Table Row myBuilder.Append("</table>"); return myBuilder.ToString(); } //****** Write to event log for Stall ****** public void writeEventStall(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 100); } //****** Write to event log checking CUST loaded ****** public void writeEventCUSTLoaded(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 16); } //****** Write to event log checking PDTHLD loaded ****** public void writeEventPDTHLDLoaded(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 17); } //****** Write to event log for ACCT hash ****** public void writeEventACCTHash(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 18); } //****** Write to event log for CUST hash ****** public void writeEventCUSTHash(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 19); } //****** Write to event log for PDTHLD hash ****** public void writeEventPDTHLDHash(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 20); } //****** Write to event log for PDTHLD hash ****** public void writeEventACCTEvent(string message) { if (!EventLog.SourceExists("PBEMonitoring")) { EventLog.CreateEventSource("PBEMonitoring", "Application"); } EventLog.WriteEntry("PBEMonitoring", message, EventLogEntryType.Error, 21); } //****** Write to application log ****** public void appLog(string message) { //Get XML Email XmlDocument doc = new XmlDocument(); doc.Load("c:\MASTER.config"); //Basic Emails XmlNode root1 = doc.DocumentElement["logLocation"]; logLocation = root1.FirstChild.InnerText; //Log Email Sent System.IO.StreamWriter file = new System.IO.StreamWriter(logLocation, true); file.Write(message); file.WriteLine(); file.Close(); } } } 

submitted by /u/IAmNotRedJohn
[link] [comments]

Leave a Reply