EWS中BindToRecurringMaster获取定期主约会ID过慢,求高效方法
优化EWS获取定期主约会ID的性能问题
我之前也碰到过这个问题——每个定期约会实例都单独调用BindToRecurringMaster发起EWS请求,确实会导致速度慢到难以接受。其实我们可以在初始查询约会列表时就一次性获取主约会ID,完全不用额外的请求。
核心思路
EWS的Appointment对象本身就包含一个RecurringMasterId属性,我们只需要在查询约会时,把这个属性加入到请求的PropertySet中,就能直接从返回的约会实例里拿到主约会ID,无需再单独绑定主约会。
优化后的代码示例
// 定义要获取的属性集合:包含基础属性 + RecurringMasterId PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly) { AppointmentSchema.RecurringMasterId }; // 构建时间段查询条件 CalendarView calendarView = new CalendarView(startDate, endDate); calendarView.PropertySet = propertySet; // 一次性获取时间段内的所有约会 FindItemsResults<Item> results = Service.FindItems(WellKnownFolderName.Calendar, calendarView); // 遍历处理每个约会 foreach (Item item in results.Items) { if (item is Appointment appointment) { if (appointment.IsRecurring && appointment.RecurringMasterId != null) { // 直接拿到主约会ID,无需额外请求 ItemId masterId = appointment.RecurringMasterId; // 后续逻辑处理... } else { // 非定期约会,直接使用自身ID ItemId appointmentId = appointment.Id; // 后续逻辑处理... } } }
关键优势
- 把原来的N次EWS请求(N为定期约会实例数量)减少到1次请求,性能提升非常明显
- 代码逻辑更简洁,避免了多次绑定的冗余操作
需要注意的是,只有定期约会的实例才会有RecurringMasterId属性,普通约会和主约会本身这个属性会是null,所以处理时要加判断。
内容的提问来源于stack exchange,提问作者synic
相关产品推荐
相关产品推荐

