SecurityException when enumerating V1 tasks and displaying tasks' info using a DataGrid

Topics: Errors
Jul 2, 2013 at 9:35 PM
I am facing a weird issue. I am trying to enumerate tasks in multiple remote servers (V1 and V2) and then display the tasks in a datagrid. This issue happens when I am connecting to a V1 server (using forceV1=true). The enumeration works fine - the data is passed to the grid even. But as soon as I move my cursor over the grid, I get a SecurityException saying 'Requested registry access is not allowed.' Does anyone know what's going on?
public partial class MainWindow : Window
{
        public MainWindow()
        {
                String osVer;
                using (var reg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "deves07"))
                using (var key = reg.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\"))
                {
                        osVer = string.Format("Name:{0}, Version:{1}", key.GetValue("ProductName"), key.GetValue("CurrentVersion"));
                }
                TaskService ts;
                if (osVer.Contains("2003"))
                {
                        ts = new TaskService("REMOTE", "username", "domain", "password", true);
                }
                else
                {
                        ts = new TaskService("REMOTE", "username", "domain", "password");
                }
                List<TaskInfo> taskList = new List<TaskInfo>();
                EnumFolderTasks(ts.RootFolder, taskList);
                DataContext = taskList;
        }

        void EnumFolderTasks(TaskFolder fld, List<TaskInfo> taskLst)
        {
                Debug.WriteLine(fld.Name);
                foreach (Task task in fld.Tasks)
                        taskLst.Add(ActOnTask(task));
                foreach (TaskFolder sfld in fld.SubFolders)
                        EnumFolderTasks(sfld, taskLst);
        }

        TaskInfo ActOnTask(Task t)
        {
                TaskInfo taskInfo = new TaskInfo();
                taskInfo.Name = t.Name;
                taskInfo.isRunning = t.IsActive;
                taskInfo.NextRunTime = t.NextRunTime;
                taskInfo.LastRunTime = t.LastRunTime;
                //taskInfo.LastRunStatus = t.LastTaskResult;
                return taskInfo;
        }

}

public class TaskInfo
{
        public string Name { set; get; }
        public bool isRunning { set; get; }
        public DateTime NextRunTime { set; get; }
        public DateTime LastRunTime { set; get; }
        public int LastRunStatus { set; get; }
}
Coordinator
Jul 3, 2013 at 3:09 AM
First, try putting brackets in around the scope of your first "using" statement. You can also do the same with the TaskService instance. You could do this by doing:
List<TaskInfo> taskList = new List<TaskInfo>();
using (TaskService ts = new TaskService(REMOTE", "username", "domain", "password", osVer.Contains("2003")))
{
    EnumFolderTasks(ts.RootFolder, taskList);
}
DataContext = taskList;
Jul 3, 2013 at 1:20 PM
Localizing the TaskService call works like a charm. Thanks a ton!

I am still unsure why this would happen only on V1 calls though - any idea there?
Coordinator
Jul 4, 2013 at 12:15 AM
My only guess is that there is something different in the way .NET is releasing the underlying COM objects for the two libraries is different.