使用OutlookServicesClient时SystemOutOfMemoryException

时间:2016-02-02 21:39:44

标签: c# office365 office365api microsoft-graph office365-restapi

我最近开始使用新的Outlook / Office 365 Rest API,并使用authentication获得了scopes(新的v2.0应用模型)OutlookServicesClient的一些很好的帮助写得更好(linq-)queries

不幸的是,有些用户报告了一些内存问题,导致以下System.OutOfMemoryException

    Exception of type 'System.OutOfMemoryException' was thrown. :    at Microsoft.OData.Core.Json.JsonReader.ReadInput()
       at Microsoft.OData.Core.Json.JsonReader.ParseStringPrimitiveValue(Boolean& hasLeadingBackslash)
       at Microsoft.OData.Core.Json.JsonReader.ParseValue()
       at Microsoft.OData.Core.Json.JsonReader.Read()
       at Microsoft.OData.Core.Json.BufferingJsonReader.ReadInternal()
       at Microsoft.OData.Core.Json.BufferingJsonReader.ReadNextAndCheckForInStreamError()
       at Microsoft.OData.Core.Json.BufferingJsonReader.ReadInternal()
       at Microsoft.OData.Core.Json.BufferingJsonReader.Read()
       at Microsoft.OData.Core.JsonLight.ODataJsonLightDeserializer.ParseProperty(DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func`2 readPropertyAnnotationValue, String& parsedPropertyName)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightDeserializer.ProcessProperty(DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func`2 readPropertyAnnotationValue, Action`2 handleProperty)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadComplexValue(IEdmComplexTypeReference complexValueTypeReference, String payloadTypeName, SerializationTypeNameAnnotation serializationTypeNameAnnotation, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadNonEntityValueImplementation(String payloadTypeName, IEdmTypeReference expectedTypeReference, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, CollectionWithoutExpectedTypeValidator collectionValidator, Boolean validateNullValue, Boolean isTopLevelPropertyValue, Boolean insideComplexValue, String propertyName, Nullable`1 isDynamicProperty)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightEntryAndFeedDeserializer.ReadEntryDataProperty(IODataJsonLightReaderEntryState entryState, IEdmProperty edmProperty, String propertyTypeName)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightEntryAndFeedDeserializer.ReadEntryPropertyWithValue(IODataJsonLightReaderEntryState entryState, String propertyName)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightEntryAndFeedDeserializer.<>c__DisplayClass2.<ReadEntryContent>b__0(PropertyParsingResult propertyParsingResult, String propertyName)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightDeserializer.ProcessProperty(DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func`2 readPropertyAnnotationValue, Action`2 handleProperty)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightEntryAndFeedDeserializer.ReadEntryContent(IODataJsonLightReaderEntryState entryState)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightReader.ReadEntryStart(DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, SelectedPropertiesNode selectedProperties)
       at Microsoft.OData.Core.JsonLight.ODataJsonLightReader.ReadAtEntryEndImplementationSynchronously()
       at Microsoft.OData.Core.JsonLight.ODataJsonLightReader.ReadAtEntryEndImplementation()
       at Microsoft.OData.Core.ODataReaderCore.ReadImplementation()
       at Microsoft.OData.Core.ODataReaderCore.ReadSynchronously()
       at Microsoft.OData.Core.ODataReaderCore.InterceptException[T](Func`1 action)
       at Microsoft.OData.Core.ODataReaderCore.Read()
       at Microsoft.OData.Client.Materialization.ODataReaderWrapper.Read()
       at Microsoft.OData.Client.Materialization.FeedAndEntryMaterializerAdapter.TryRead()
       at Microsoft.OData.Client.Materialization.FeedAndEntryMaterializerAdapter.TryStartReadFeedOrEntry()
       at Microsoft.OData.Client.Materialization.FeedAndEntryMaterializerAdapter.TryReadEntry(MaterializerEntry& entry)
       at Microsoft.OData.Client.Materialization.FeedAndEntryMaterializerAdapter.<LazyReadEntries>d__0.MoveNext()
       at Microsoft.OData.Client.Materialization.FeedAndEntryMaterializerAdapter.Read()
       at Microsoft.OData.Client.Materialization.ODataReaderEntityMaterializer.ReadNextFeedOrEntry()
       at Microsoft.OData.Client.Materialization.ODataEntityMaterializer.ReadImplementation()
       at Microsoft.OData.Client.MaterializeAtom.MoveNextInternal()
       at Microsoft.OData.Client.MaterializeAtom.MoveNext()
       at System.Linq.Enumerable.<CastIterator>d__94`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Microsoft.OData.ProxyExtensions.PagedCollection`2..ctor(DataServiceContextWrapper context, QueryOperationResponse`1 qor)
       at Microsoft.OData.ProxyExtensions.DataServiceContextWrapper.<>c__DisplayClass38`2.<ExecuteAsync>b__36(IAsyncResult r)
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.OData.ProxyExtensions.DataServiceContextWrapper.<ExecuteAsync>d__3a`2.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
       at Shared.Data.Office365.Office365Api.<GetEmailsReceived>d__23.MoveNext()

我的查询(获取日期收到的电子邮件数量)如下所示:

var dtStart = date.Date.ToUniversalTime();
var dtEnd = date.Date.AddDays(1).ToUniversalTime();

var mailResults = await _client.Me.Messages
    .OrderByDescending(m => m.ReceivedDateTime)
    .Where(m => m.ReceivedDateTime.Value >= dtStart && m.ReceivedDateTime.Value <= dtEnd)
    //todo: filter if not in Junk Email and Deleted Folder (maybe with ParentFolderId)
    .Take(500) // todo: add paging to (and maybe make more efficient?)
    .Select(m => new DisplayEmail(m)).ExecuteAsync();

// todo: add paging to (and maybe make more efficient?)
foreach (var message in mailResults.CurrentPage)
{
    emails.Add(message);
}

我没有找到处理源的方法,也认为没有必要,因为只有局部变量。然后将emails - 列表的结果存储在数据库中。在添加此功能之前,我的工具的内存使用情况稳定,现在每小时增长大约10Mb(取决于有人发送了多少封电子邮件)。

非常感谢任何建议!

1 个答案:

答案 0 :(得分:2)

你可以在这里尝试一些事情:

  • 缩小页面大小。你在这里一次做500(.Take(500)),你可以尝试减少它。
  • 仅请求您感兴趣的属性。如果您修改.Select以引用m变量的特定属性,则API应在API中使用$select查询参数要求限制回来的东西。有关示例,请参阅this tutorial