Running TaskScheduler from WebAPI - unable to schedule tasks

73 views
Skip to first unread message

santiago...@gmail.com

unread,
Mar 3, 2020, 11:36:38 AM3/3/20
to Task Scheduler Managed Wrapper
Hello,

I am running a WebAPI controller that is trying to run a scheduled task.
This is a custom command that receives a simple parameter.

I use the following code inside the controller with [HttpPost] attribute: 


           using (var ts = new Microsoft.Win32.TaskScheduler.TaskService())
            {
                // Create a new task definition and assign properties
                var td = ts.NewTask();
                td.RegistrationInfo.Description = input.Description;

                // Create a trigger that will fire the task at this time every other day
                td.Triggers.Add(new Microsoft.Win32.TaskScheduler.DailyTrigger { DaysInterval = 2, StartBoundary = DateTime.Now.AddMinutes(1) });


                // Create an action that will launch Notepad whenever the trigger fires
                //td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction(qsClient, @"/key:" + retVal.ToString(), null));
                td.Actions.Add(new Microsoft.Win32.TaskScheduler.ExecAction("dir.exe","C:\\temp\\", null));

                // Register the task in the root folder
                //string taskName = input.OutputName.Replace("%DATE%", retVal.ToString());
                string taskName = "MyDirectory";
                ts.RootFolder.RegisterTaskDefinition(taskName, td);
            }


but when trying to execute this, I got the following error, I tried several other options but always end to this point:
    "ExceptionMessage""(43,4):Task:",
    "ExceptionType""System.Runtime.InteropServices.COMException",
    "StackTrace""   at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String path, TaskDefinition definition, TaskCreation createType, String userId, String password, TaskLogonType logonType, String sddl)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String path, TaskDefinition definition)


Are you guys familiar with this issue? My WebAPI service is running under IIS using an application pool with ApplicationPoolIdentity.

Thank you

santiago...@gmail.com

unread,
Mar 3, 2020, 12:03:37 PM3/3/20
to Task Scheduler Managed Wrapper
I changed the Identity of the application pool from ApplicationPoolIdentity to LocalServices and it works.
Reply all
Reply to author
Forward
0 new messages