我的系统Windows server 2012 - iis 8 - vs2015 我创建了一个页面(aspx web表单,C#) 允许最终用户指定报告,电子邮件并选择通过电子邮件接收报告的时间我使用TaskScheduler 2.7.2 NuGet Link 创建一个任务计划来做到这一点 问题如果我在本地测试它工作正常但发布时它返回该错误
{Message: "(47,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)
↵ at slnAlabraq.Query.CreateTask(Int32 ID, String Time) in C:\Sources\slnAlabraq\slnAlabraq TFS\slnAlabraq\Query.asmx.cs:line 7655
↵ at slnAlabraq.Query.SaveRepSch(Int32 id, String Time, String Reports, String Emails) in C:\Sources\slnAlabraq\slnAlabraq TFS\slnAlabraq\Query.asmx.cs:line 7812"
代码
private string CreateTask(int ID, string Time)
{
// Get the service on the local machine
try
{
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Send Report By Email Schedule ID" + ID;
td.Principal.LogonType = TaskLogonType.InteractiveToken;
// Add a trigger that will fire the task at this time every other day
DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger(1));
dt.Repetition.Duration = TimeSpan.FromDays(5000);
dt.Repetition.Interval = TimeSpan.FromDays(1);
dt.StartBoundary = DateTime.Parse(Time);
// dt.EndBoundary = DateTime.Parse(Time).AddMinutes(20);
// Add an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("C:\\Projects\\Report_Emails\\Report_Emails\\bin\\Debug\\Report_Emails.exe"));
// Register the task in the root folder
string taskName = "Send Report " + ID.ToString();
//Error Here
ts.RootFolder.RegisterTaskDefinition(taskName, td);
}
return "";
}
catch (Exception ex) { return ex.ToString(); }
}
我认为这是许可问题,因为它适用于本地 如果是允许远程用户创建任务的权限
谢谢
答案 0 :(得分:0)
如上所述,这将在网站用户下运行,设置为。
鉴于您需要授予特定权限,我建议拥有专门的用户进行任务管理。您可以在创建TaskService
时传递此用户,即
using (TaskService ts = new TaskService(serverName, taskUser, taskDomain, taskPassword))