我在 WebApplication-Scope 下获得了一个 SharePoint-Feature,它应该创建一个 Timer-Job。
在功能激活(在创建该作业之前)和功能取消激活时,如果该作业已经存在,我想删除它。这是功能激活代码:
public class myJob : SPJobDefinition
public myJob() : base("JobName", SPAdministrationWebApplication.Local, null, SPJobLockType.Job) {}
}
public class JobFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication application = properties.Feature.Parent as SPWebApplication;
// 1. Delete old Job if exists
foreach (SPJobDefinition job in application.JobDefinitions)
{
if (string.Equals(job.Name, "JobName"))
{
job.Delete(); // NEVER GETS HERE
}
}
// 2. Install job
myJob deploymentJob = new myJob();
SPMinuteSchedule schedule = new SPMinuteSchedule { BeginSecond = 0, EndSecond = 59, Interval = 5 };
deploymentJob.Schedule = schedule;
deploymentJob.Update(); // CRASHES
}
}
问题是:永远不会到达应该删除作业的代码。这份工作似乎不在 application.JobDefinitions 中。(上面代码中的“// 永远不会到达这里”)
但是当我尝试创建部署作业时,我得到了一个作业已经存在的异常(上面代码中的“// CRASHES”):
{"An object of the type myJob named \"JobName\" already exists under the parent Microsoft.SharePoint.Administration.SPAdministrationWebApplication named \"\". Rename your object or delete the existing object."}
错误在哪里?