“由于对象的当前状态,操作无效”部署 sharepoint 事件接收器时
我有一个简单的事件接收器,当我创建构造函数时,我在部署步骤中遇到错误,我仔细检查了功能、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);
}
这是我的 stackTrace:
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...
I have a simple event receiver, when I create a constructor I got the error in deployment step, I double checked the event receiver scope in feature, Elements.xml file and my code, the weird thing is when I remove the constructor(where I use service locator to get an instance of my implementation class) it works fine.
my code is something like this:
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);
}
here is my stackTrace:
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您无法使用 SharePoint 警报功能,并且不需要可以轻松发送电子邮件的工作流,则应删除构造函数并将其代码放入 ItemAdded 方法中。
If you cannot use SharePoint Alert functionality and you do not need a workflow that can also send an email with minimum efforts you should remove the constructor and put its code into the ItemAdded method.