1

我创建了一个自定义计时器作业并通过 sharepoint 中的功能事件实现它。

当我试图激活它时,它显示了这个特定的错误:

Object reference not set to an instance of an object.   at TimerJobExample.MyFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties) in C:\Documents and Settings\admin-shuklag.INFRADEV\My Documents\Visual Studio 2008\Projects\TimerJobExample\TimerJobExample\MyFeatureReceiver.cs:line 22 
   at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce) 
   at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce) 
   at Microsoft.SharePoint.SPFeatureCollection.AddInternal(Guid featureId, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly) 
   at Microsoft.SharePoint.SPFeatureCollection.Add(Guid featureId) 
   at Microsoft.SharePoint.WebControls.FeatureActivator.BtnActivateFeature_Click(Object objSender, EventArgs evtargs) 
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

错误行在下面突出显示

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPSite site = properties.Feature.Parent as SPSite;

        // make sure the job isn't already registered


//error line
      **foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)**

        {

            if (job.Name == List_JOB_NAME)

                job.Delete();

        }



        SampleTimer listLoggerJob = new SampleTimer(List_JOB_NAME, site.WebApplication);

        SPMinuteSchedule schedule = new SPMinuteSchedule();

        schedule.BeginSecond = 0;

        schedule.EndSecond = 59;

        schedule.Interval = 5;

        listLoggerJob.Schedule = schedule;

        listLoggerJob.Update();

    }

可能是什么问题呢?

4

2 回答 2

5

根据您的功能接收器,我只能看到两种可能性:

  1. 仔细检查 feature.xml 中的 Scope 属性是否设置为 Site。如果设置为 Web,则Object Reference not set在尝试使用site变量时会出现异常。
  2. 正如 Janis 所说,查看MyCustomJob构造函数内部。确保所有变量都已正确初始化。
于 2011-09-19T12:55:33.090 回答
1

问题在于功能的范围。它被设置为 Web,并且要在此级别激活功能,您需要一个管理员帐户或具有提升权限的帐户。

您可以将应用程序池 ID 添加到场管理员帐户或以提升的权限运行代码。

于 2011-09-26T04:06:42.523 回答