(Edit) I'm not asking what a NullReferenceException is. This is not a duplicate. The issue is regarding System.Task...
I have a Windows service running on Server 2003 SP2 that has been running fine for over a year. The service just runs a timer and launches various tasks throughout the day. Several of these tasks are long-running processes, so the service uses a System.Task to launch the long-running tasks so they don't block the main timer thread. Some of the task now fail every time, right after they are started. All tasks launch and run fine in the VS2013 IDE, but fail when deployed in the server.
This is the code from the Windows service timer elapsed method that is hit when it's time to run the async task:
if (!IsTaskRunning(processConfig.Name)) { _log.Write("Executing process " + processConfig.Name + "(" + occurance.Name + ") asyncronously.");
AsyncTask task = new AsyncTask(processConfig, occurance, Task<ProcessResult>.Factory.StartNew(() => process.Run()), processConfig.Name);
_asyncTasks.Add(task); }
The AsyncTask object is my own creation that just wraps the System.Task object with a few other pieces of information needed to report the task status when it finishes. The task fails immediately after creation (it starts execution immediately). This is the exception and stack trace:
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at OnePoint.Automation.ServiceBase.<>c__DisplayClass6.b__3() at System.Threading.Tasks.Task
1.InvokeFuture(Object futureAsObj)
1.InvokeFuture(Object futureAsObj)
at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at OnePoint.Automation.ServiceBase.<>c__DisplayClass6.<CheckForTasksToRun>b__3() at System.Threading.Tasks.Task
at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute()<---
I have tried other methods of starting the task, ie. instantiating the task and AsyncTask object, then calling Task.Start() after, but the behavior is the same. I even placed logging inside the actual process.Run() method to see if the code ever gets reached, and it does not.
Any tips/help would be appreciated. This problem has wasted so much of my time already.