在Exchange Web Services(EWS)中处理事件时识别项目类型 [英] Identify item type when handling event in Exchange Web Services (EWS)

查看:96
本文介绍了在Exchange Web Services(EWS)中处理事件时识别项目类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在EWS API中使用流式通知.在事件处理程序上,我了解到一个项目已被修改的事实,但是将修改后的项目绑定到电子邮件的尝试失败.错误消息具体是

I'm using streaming notifications with the EWS API. On the event handler I pick up the fact that an item has been modified, but my attempt to bind the modified item to an email message fails. The error message is specifically

服务(约会)返回的商品类型不兼容 带有请求的项目类型(EmailMessage).

The item type returned by the service (Appointment) isn't compatible with the requested item type (EmailMessage).

似乎在尝试绑定项目之前必须有一种方法来标识项目类型,但是我不确定那是什么.尝试Bind时发生错误,因此我不能简单地检查是否为null.我可以求助于try/catch,但是如果有更好的方法,宁愿正确执行此操作?

It seems like there must be a way to identify the item type before attempting to bind it, but I'm not sure what that is. The error occurs on attempting to Bind, so I can't simply check for null. I could resort to try/catch, but would prefer to do this properly if there is a better way?

摘要代码:

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类型.为了稳健地执行此操作(在将项目绑定之前处理可能发生的问题,将其移动),我将逻辑放入方法中,如下所示:

Looks like the correct way to bind is to use the generic Item.Bind method, then check if the item is an EmailMessage type. To do this robustly (handle potential issues where the item is moved before it can be bound) I put the logic into a method, similar to below:

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

然后将现有方法中的逻辑更改为

Then change the logic in my existing method to

EmailMessage modifiedMessage = BindToEmailMessage(itemEvent.ItemId);
if (modifiedMessage != null) ...

这篇关于在Exchange Web Services(EWS)中处理事件时识别项目类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆