ExchangeService 查找项目突然间歇性地无法正常工作



这已经完美运行了大约一年或更长时间,然后突然间,我认为从 10 月 12 日星期六开始失败(FindResults 不返回任何项目(;

//Tag the sent email so we can pull it back in a moment
Guid myPropertySetId = new Guid("{375a1079-a049-4c2d-a2e1-983d588cbed4}");
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "TelEmailGuid", MapiPropertyType.String);
Guid telEmailGuid = Guid.NewGuid();
message.SetExtendedProperty(myExtendedPropertyDefinition, telEmailGuid.ToString());
//Send the email
message.SendAndSaveCopy(completedFolder);
//Find the sent email
ItemView view = new ItemView(1);
SearchFilter searchFilter = new SearchFilter.IsEqualTo(myExtendedPropertyDefinition, telEmailGuid.ToString());
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
FindItemsResults<Item> findResults = service.FindItems(completedFolder, searchFilter, view);
return DownloadEmail(findResults.FirstOrDefault().Id.ToString());

在尝试查找电子邮件之前,我尝试稍微调整一下等待,这有所帮助(现在可能有 10% 成功(。因此,我随后添加了一个循环,因此如果没有找到,它将重试几次。但似乎如果第一次没有找到它,那么在随后的尝试中也不会找到它;

//Tag the sent email so we can pull it back in a moment
Guid myPropertySetId = new Guid("{375a1079-a049-4c2d-a2e1-983d588cbed4}");
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "TelEmailGuid", MapiPropertyType.String);
Guid telEmailGuid = Guid.NewGuid();
message.SetExtendedProperty(myExtendedPropertyDefinition, telEmailGuid.ToString());
//Send the email
message.SendAndSaveCopy(completedFolder);
//Find the sent email
ItemView view = new ItemView(1);
SearchFilter searchFilter = new SearchFilter.IsEqualTo(myExtendedPropertyDefinition, telEmailGuid.ToString());
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
int attempt = 1;
System.Threading.Thread.Sleep(1000);
FindItemsResults<Item> findResults = service.FindItems(completedFolder, searchFilter, view);
while (findResults.TotalCount == 0 && attempt < 5)
{
findResults = service.FindItems(completedFolder, searchFilter, view);
attempt++;
}
return DownloadEmail(findResults.FirstOrDefault().Id.ToString());

有人有什么建议吗?我怀疑这是一个Microsoft问题,但也许不同的方法可能会让我们解决这个问题。

这听起来像是搜索超时的问题,因为随着文件夹项目计数的增长,您不搜索索引属性,搜索的性能将随着时间的推移而下降(此外,其他因素如服务器负载等将在具有大量项目计数的文件夹中搜索项目时产生直接影响(。

您的搜索看起来非常静态,因此您可以创建一个搜索文件夹 https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633690(v%3Dexchg.80(,这将优化搜索。

您可能可以做的另一件事是为搜索添加日期时间限制,例如,我有基于 Internet MessageId 搜索消息的代码,当项目计数变大时,它也有类似的问题搜索超时。所以因为我知道我正在搜索的总是最近的电子邮件添加日期时间限制,在这种情况下解决了问题,例如

SearchFilter internetMessageIdFilter = new SearchFilter.IsEqualTo(PidTagInternetMessageId, InternetMessageId);
SearchFilter DateTimeFilter = new SearchFilter.IsGreaterThan(EmailMessageSchema.DateTimeReceived, DateTime.Now.AddDays(-1));
SearchFilter.SearchFilterCollection searchFilterCollection= new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(internetMessageIdFilter);
searchFilterCollection.Add(DateTimeFilter);

最新更新