Hope I can get some help here.
On a DEV server I created a C# windows service in Visual Studio 2013 Community. I have tested it in debug mode: In Main()
#if DEBUG
...run debug code...
#else
...run service code...
#endif
In debug mode it runs perfectly fine. I then added an installer class and successfully installed the service on the same server and started it in the Services window. However, it doesn't do anything. I checked the Event log and saw this error message:
Application: SharenetIFF.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at SharenetIFF.RunValues.GetRunValues()
at SharenetIFF.SearchFiles.LookforIFFFiles(Int32)
at SharenetIFF.Program.DoThis()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
Here is the code in RunValues:
class RunValues
{
public int id { get; set; }
public int runTimeSpan { get; set; }
public int numberOfRunTime { get; set; }
private SqlConnection myCon = new SqlConnection();
public List<int> GetRunValues()
{
List<int> values = null;
string destPath = "";
try
{
string mySQL = "select RunFreq, RunTimes from IFFRunValues";
myCon.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConn"].ConnectionString;
myCon.Open();
SqlCommand myCmd = new SqlCommand(mySQL, myCon);
SqlDataReader runValuesReader = myCmd.ExecuteReader();
if (runValuesReader.HasRows)
{
while (runValuesReader.Read())
{
runTimeSpan = Convert.ToInt16(runValuesReader["RunFreq"]);
numberOfRunTime = Convert.ToInt16(runValuesReader["RunTimes"]);
}
values = new List<int>();
values.Add(runTimeSpan);
values.Add(numberOfRunTime);
}
runValuesReader.Close();
myCon.Close();
runValuesReader.Dispose();
myCmd.Dispose();
myCon.Dispose();
}
catch (Exception ex)
{
destPath = Path.Combine("C:\\", "error_log.txt");
File.AppendAllText(destPath, ex.Message);
values.Clear();
}
return values;
}
}
I am thinking it is failing on connection string, mostly because there is nothing else here. But no idea why. All the code is in the try/catch blocks, so how is an unhandled exception even possible? If the service is released to the same development machine it was developed on, does a service need different permissions if running outside of visual studio?