简体   繁体   English

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

[英]When deleting timer job it throws error 'Object reference not set to an instance of an object'

I have created a timer job with following code: 我用以下代码创建了一个计时器作业:

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
        }
    }
}

I then created a Site scoped feature. 然后,我创建了一个站点范围内的功能。 In its event receiver, on deactivation, I have the following 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();
        }
    }
}

But it throws me error. 但这使我出错。 From debugging I understand that the error is thrown on the line job.Delete(); 通过调试,我了解到该错误是在行job.Delete();上抛出的job.Delete(); . The error is: 错误是:

Object reference not set to an instance of an object. 你调用的对象是空的。

This is kind of confusing to me as the statement job.Name executes flawlessly and even gets me the right timer job object. 语句job.Name会让我感到困惑, job.Name可以完美执行,甚至为我提供正确的计时器作业对象。 But while trying to delete the job it throws me error. 但是,在尝试删除作业时,会引发我错误。

I found this question with similar problem as mine but the suggestions didn't not work for me. 我发现这个问题与我的问题类似,但建议对我不起作用。 From this discussion I tried by writing deletion code in SPSecurity.RunWithElevatedPrivileges but it still didn't work for me. 此讨论中,我尝试通过在SPSecurity.RunWithElevatedPrivileges编写删除代码来进行SPSecurity.RunWithElevatedPrivileges但对我而言仍然无效。

Anyone knows why this happens and how to resolve this? 有谁知道为什么会这样以及如何解决呢?

Update 1 更新1

A little more details. 更多细节。

NullReferenceException was unhandled by user code 用户代码未处理NullReferenceException

Object reference not set to an instance of an object. 你调用的对象是空的。

Here's the stack trace 这是堆栈跟踪

   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)

To delete the job use powershell: 要删除作业,请使用powershell:

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

then run: 然后运行:

iisreset /noforce

In the configuration database you need to grant those permission to let deletion work: 在配置数据库中,您需要授予这些权限以使删除工作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Quartz作业运行时出现错误,“对象引用未设置为对象的实例” - Error when a Quartz job runs with “Object Reference not set to an instance of an object” Telerik Radgrid rebind()抛出未将对象引用设置为对象错误的实例 - Telerik radgrid rebind() throws Object reference not set to instance of object error FirstOrDefault()抛出未设置为对象实例错误的对象引用 - FirstOrDefault() throws Object reference not set to an instance of an object error 图片框始终抛出未设置为对象错误实例的对象引用 - Picture box always throws object reference not set to instance of object error 编辑抛出错误:对象引用未设置为对象的实例 - Editing throws error : Object Reference not set to an instance of an object NHibernate Evict抛出未设置为对象错误实例的对象引用 - NHibernate Evict throws an Object reference not set to an instance of an object error 对象引用未设置为对象的实例(定时器数组) - Object reference not set to an instance of an object (timer array) 错误当检查Facebook喜欢时,对象引用未设置为对象的实例? - error Object reference not set to an instance of an object when check Facebook Like? 运行应用程序时出现“对象引用未设置为对象实例”错误 - “Object reference not set to an instance of an object” error when running app 运行webform时,对象引用未设置为对象错误标志的实例 - Object reference not set to an instance of an object error flags when running webform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM