删除计时器作业时,它会引发错误"对象引用未设置为对象的实例"



我创建了一个计时器作业,代码如下:

namespace EmployeeDemo
{
    class DeleteEmployees : SPJobDefinition
    {
        public DeleteEmployees() : base() { }
        public DeleteEmployees(string jobName, SPWebApplication webapp)
            : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Delete Employees Timer Job";
        }
        public override void Execute(Guid targetInstanceId)
        {
            // Code
        }
    }
}

然后,我创建了一个站点范围的功能。在其事件接收器中,在停用时,我有以下代码:

const string timerJobName = "Delete Employees Timer Job";
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    DeleteJob(properties.Feature.Parent as SPSite);
}
private void DeleteJob(SPSite site)
{
    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
    {
        if (job.Name == timerJobName)
        {
            job.Delete();
        }
    }
}

但这让我犯了一个错误。通过调试,我了解到错误是在job.Delete();行上抛出的。错误为:

对象引用未设置为对象的实例。

这让我有点困惑,因为语句job.Name完美地执行,甚至为我提供了正确的计时器作业对象。但当我试图删除该作业时,却出现了错误。

我发现这个问题和我的问题类似,但建议对我不起作用。从这次讨论中,我尝试在SPSecurity.RunWithElevatedPrivileges中编写删除代码,但它仍然对我无效。

有人知道为什么会发生这种情况,以及如何解决这种情况吗?

更新1

更多细节。

用户代码未处理NullReferenceException

对象引用未设置为对象的实例。

这是堆栈跟踪

   at Microsoft.SharePoint.Administration.SPConfigurationDatabase.DeleteObject(Guid id)
   at Microsoft.SharePoint.Administration.SPConfigurationDatabase.Microsoft.SharePoint.Administration.ISPPersistedStoreProvider.DeleteObject(SPPersistedObject persistedObject)
   at Microsoft.SharePoint.Administration.SPPersistedObject.Delete()
   at Microsoft.SharePoint.Administration.SPJobDefinition.Delete()
   at EmployeeDemo.Features.DeleteEmpFeature.DeleteEmpFeatureEventReceiver.DeleteJob(SPSite site)
   at EmployeeDemo.Features.DeleteEmpFeature.DeleteEmpFeatureEventReceiver.FeatureDeactivating(SPFeatureReceiverProperties properties)
   at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)

要删除作业,请使用powershell:

$jobToDelete = Get-SPTimerJob -WebApplication "http://intranet" -Identity "YourJobName"
$jobToDelete.Delete()

然后运行:

iisreset /noforce

在配置数据库中,您需要授予这些权限才能进行删除:

use [Sharepoint_Config]
GO
GRANT EXECUTE ON [dbo].[proc_putObjectTVP] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putObject] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putDependency] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_putClass] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_getNewObjects] TO [WSS_Content_Application_Pools]
GRANT EXECUTE ON [dbo].[proc_dropObject] TO [WSS_Content_Application_Pools]
GO

相关内容

  • 没有找到相关文章

最新更新