[英]Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low
[英]Dynamics CRM Xrm Tooling error “Failed to allocate a managed memory buffer of 2147483647 bytes.”
我創建了一個XRMTool(WPF),以將Dynamics CRM Online中的數據提取到XML文件。 輸出文件可能有一百萬行,但是出現以下錯誤。
無法分配2147483647字節的托管內存緩沖區。 可用內存量可能不足。
我可以添加一些過濾器並減少行數,但是根據應用程序的要求,我必須一次提取至少一個月的數據,大約一百萬行。
如何在WPF應用程序中增加內存緩沖區?
以下是我的代碼
string fetchaudit = @"<fetch version='1.0' mapping='logical' output-format='xml-platform'>
<entity name='audit'>
<attribute name='action' />
<attribute name='auditid' />
<attribute name='callinguserid' />
<attribute name='objecttypecode' />
<attribute name='createdon' />
<attribute name='objectid' />
<attribute name='objectid' />
<attribute name='operation' />
<attribute name='regardingobjectid' />
<attribute name='transactionid' />
<attribute name='userid' />
<filter type='and'>
<condition attribute='createdon' operator='on-or-after' value='" + Fromdate + @"' />
<condition attribute='createdon' operator='on-or-before' value='" + Todate + @"' />
</filter>
</entity>
</fetch>";
while (true)
{
string xml = CreateXml(fetchaudit, pagingCookie, pageNumber, fetchCount);
RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml) // Error occurs here when page number reaches 100
};
EntityCollection returnCollection = ((RetrieveMultipleResponse)_ctrl.CrmConnectionMgr.CrmSvc.Execute(fetchRequest1)).EntityCollection;
// var collection = _ctrl.CrmConnectionMgr.CrmSvc.GetEntityDataByFetchSearchEC(xml);
if (returnCollection != null)
{
if (returnCollection.Entities.Count >= 0)
Entities.AddRange(returnCollection.Entities);
}
if (returnCollection.MoreRecords)
{
pageNumber++;
pagingCookie = returnCollection.PagingCookie;
}
else
{
break;
}
} // end while
我正在使用SDK中的以下方法來分頁SampleCode \\ CS \\ GeneralProgramming \\ Queries \\ FetchPagingWithCookie.cs( https://msdn.microsoft.com/zh-cn/library/gg309717.aspx )
如果是這樣,則很可能不會在第一個請求上發生異常,從而減少返回的行數。
您當前的問題是您將所有內容存儲在內存中,直到檢索結束
Entities.AddRange(returnCollection.Entities);
Entities
的集合正在迅速增長,但是添加時並不會失敗,因為在實際的SDK檢索中會發生內存分配。
要使用DataContractSerializer
將查詢結果序列化為單個集合,可以使用下一個IEnumerable實現按包讀取數據( 以及代碼的更多說明 ):
public class XrmEnumerator<T> : IEnumerator<T> where T : Entity
{
private readonly Queue<T> _collected = new Queue<T>();
private IOrganizationService _service;
private QueryExpression _query;
private string _lastCookie;
private bool _moreRecords;
public T Current { get; private set; }
object IEnumerator.Current => Current;
public XrmEnumerator(IOrganizationService service, QueryExpression query)
{
_service = service;
_query = query;
if (query.PageInfo == null)
query.PageInfo = new PagingInfo
{
Count = 5000,
PageNumber = 1
};
FillThePack();
}
private void FillThePack()
{
var result = _service.RetrieveMultiple(_query);
_lastCookie = result.PagingCookie;
result.Entities.ToList().ForEach(e=>_collected.Enqueue(e.ToEntity<T>()));
_moreRecords = result.MoreRecords;
}
public void Dispose()
{
_service = null;
_query = null;
}
public bool MoveNext()
{
if(_collected.Count == 0)
{
if (!_moreRecords)
{
return false;
}
_query.PageInfo.PagingCookie = _lastCookie;
_query.PageInfo.PageNumber++;
FillThePack();
}
Current = _collected.Dequeue();
return true;
}
public void Reset()
{
_query.PageInfo.PagingCookie = string.Empty;
_query.PageInfo.PageNumber = 1;
}
}
public class XrmEnumerable<T> : IEnumerable<T> where T : Entity
{
private readonly XrmEnumerator<T> _enumerator;
public XrmEnumerable(IOrganizationService service, QueryExpression query)
{
_enumerator = new XrmEnumerator<T>(service, query);
}
public IEnumerator<T> GetEnumerator() => _enumerator;
IEnumerator IEnumerable.GetEnumerator()
{
return _enumerator;
}
public virtual void Add(T entity)
{
// do your code on item deserialization
}
}
它使用QueryExpression
而不是FetchXml
,但是您可以輕松地將其轉換
用法樣本:
var enumerable = new XrmEnumerable<Contact>(service, query);
var serializer = new DataContractSerializer(typeof(XrmEnumerable<Contact>));
using (var file = File.OpenWrite("contacts.xml"))
serializer.WriteObject(file, enumerable);
另外,您可以創建XrmEnumerable
的子類,並在Add
函數中執行邏輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.