c#-在Exchange Web Services(EWS)中处理事件时识别项目类型
作者:互联网
我正在EWS API中使用流式通知.在事件处理程序上,我收到一个事实,即项目已被修改,但是将修改后的项目绑定到电子邮件的尝试失败.错误消息是专门
The item type returned by the service (Appointment) isn’t compatible
with the requested item type (EmailMessage).
似乎在尝试绑定项目之前必须有一种方法来标识项目类型,但是我不确定那是什么.尝试绑定时发生错误,因此我不能简单地检查null.我可以尝试/捕获,但是如果有更好的方法,宁愿正确执行此操作?
汇总代码:
void streamingConnection_OnNotificationEvent(object sender, NotificationEventArgs args)
{
foreach (NotificationEvent notificationEvent in args.Events)
{
ItemEvent itemEvent = notificationEvent as ItemEvent;
if (itemEvent != null) HandleItemEvent(itemEvent);
}
}
private void HandleItemEvent(ItemEvent itemEvent)
{
switch (itemEvent.EventType)
{
case EventType.Modified:
EmailMessage modifiedMessage = EmailMessage.Bind(this.ExchangeService, itemEvent.ItemId);
// error occurs on Bind if the item type is not an EmailMessage (eg, an Appointment)
break;
}
}
解决方法:
似乎正确的绑定方法是使用通用的Item.Bind方法,然后检查该项目是否为EmailMessage类型.为了健壮地做到这一点(处理可能存在的问题,然后才能绑定项目),我将逻辑放入方法中,如下所示:
private EmailMessage BindToEmailMessage(ItemId itemId)
{
try
{
Item item = Item.Bind(this.ExchangeService, itemId);
if (item is EmailMessage) return item as EmailMessage;
else return null;
}
catch
{
return null;
}
}
然后将现有方法中的逻辑更改为
EmailMessage modifiedMessage = BindToEmailMessage(itemEvent.ItemId);
if (modifiedMessage != null) ...
标签:exchangewebservices,c 来源: https://codeday.me/bug/20191121/2053077.html