在部署SharePoint事件接收器时出现“由于对象的当前状态,操作无效”的错误。

3

我有一个简单的事件接收器,当我创建构造函数时,在部署步骤中遇到错误。我已经仔细检查了功能、Elements.xml 文件和代码中的事件接收器范围,奇怪的是,当我删除构造函数(其中我使用服务定位器来获取我的实现类的实例)时,它运行良好。

我的代码大致如下:

    private INotificationService iNotificationService;
    public CongeER()
    {
        SPSite currentSite = SPContext.Current.Site;
        IServiceLocator locator = SharePointServiceLocator.GetCurrent(currentSite);

        try
        {
            iNotificationService = locator.GetInstance<INotificationService>();
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }

   public override void ItemAdded(SPItemEventProperties properties)
   {
       iNotificationService.NotifyByMail();
       base.ItemAdded(properties);
   }

这是我的堆栈跟踪:

  Feature Activation: Threw an exception, attempting to roll back.
  Exception: System.InvalidOperationException: 
  L'opération n'est pas valide en raison de l'état actuel de l'objet.     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionContentTypeAndEventReceiverBindings(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boo...

你尝试从网站用户界面激活该功能了吗? - Maks Matsveyeu
是的,我做了,出现了以下错误:运行时错误描述:服务器上发生了应用程序错误。此应用程序的当前自定义错误设置防止查看应用程序错误的详细信息。 - Mosbah
这似乎是一个新假期的通知。您能告诉我业务场景,以便我可以为您提供符合SharePoint最佳实践的解决方案吗? - Maks Matsveyeu
业务场景非常简单:当从 Web 部件或 Web 服务发送休假请求时,我需要向经理发送电子邮件。类“iNotificationService”具有各种通知方法,包括发送电子邮件。 - Mosbah
1个回答

1
如果您无法使用SharePoint警报功能,并且不需要一个可以通过最小的努力也能发送电子邮件的工作流程,那么您应该删除构造函数并将其代码放入ItemAdded方法中。

如果我将代码从构造函数中移动,ItemAdded()函数会被调用,但是我无法从serviceLocator获取一个新实例,谢谢您的时间。 - Mosbah
对象引用未设置到对象的实例。 - Mosbah
而调用堆栈呢?可能是在从Visual Studio部署时导致错误的SPSite currentSite = SPContext.Current.Site;。尝试使用SPSite site = properties.OpenSite();。 - Maks Matsveyeu
不,现在它正在正确部署,我用SPSite currentSite = new SPSite("http://myAdress"); 替换了SPSite currentSite = SPContext.Current.Site; 但是iNotificationService.NotifyByMail(); 引起了错误,然而它并不为空! - Mosbah
没事了,我已经解决了,后来的问题是由于NotifyByMail()实现错误引起的,你知道这是周末,非常感谢您。 - Mosbah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接