I have a service that checks other services and reports information on them.
The error is:
Object reference not set to an instance of an object. at ServiceControl.ServiceCore.CheckServiceStatus(List`1 myservices)
Code Block
protected void CheckServiceStatus(List<ServiceConfig> myservices)
{
if (!restarting)
{
for (int i = 0; i < myservices.Count; i++)
{
string emailBody = "";
try
{
if (myservices[i].service.Status == ServiceControllerStatus.Running)
{
serviceName = myservices[i].service.DisplayName;
try
{
foreach (ManagementObject obj in searcher.Get())
{
uint processId = (uint)obj["ProcessId"];
Process process = null;
process = Process.GetProcessById((int)processId);
//Assign memory to service for later use
myservices[i].memoryUsage = process.WorkingSet64;
//Check process memory vs threshold
if (myservices[i].memoryUsage > myservices[i].memoryThreshold)
{
//report if memory exceeds threshold
emailBody = myservices[i].serviceName + " has exceeded its memory threshold of " + myservices[i].memoryThreshold + "\n"
+ "Current memory usage: " + Convert.ToString(myservices[i].memoryUsage);
EmailReport(emailBody, false);
//extend memory threshold by %
myservices[i].memoryThreshold *= myservices[i].memoryThresholdIncPercentage;
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.Message);
emailBody = "Could not find process for " + myservices[i].serviceName;
EmailReport(emailBody, true);
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.Message + "\n" + ex.StackTrace + "\n memory");
}
try
{
if (myservices[i].service.Status != myservices[i].status)
{
//Report change in service, includes last saved memory
emailBody = myservices[i].serviceName + " has changed status" + "\n" +
"from: " + Convert.ToString(myservices[i].status) + " to: " + Convert.ToString(myservices[i].service.Status) + "\n" +
"Memory Usage: " + Convert.ToString(myservices[i].memoryUsage);
EmailReport(emailBody, false);
}
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.Message + "\n" + ex.StackTrace + "\n status");
}
}
}
if (lastDate != DateTime.Today)
{
restarting = true;
RestartServices();
}
}
I have done a fair amount of debugging and my results are: myservices is not Null, it contains exactly 1 item in this instance.
I have also tried making a localized list within the block to consume myservices and work of the local list. this did not work.
I have tried passing myservices[i] to a local var and use the far, this did not work.
Any help would be much appreciated