[英]How to fix OutOfMemoryException when serializing to JSON string?
我有一個Class對象,其中包含作為字節數組的屬性。 此類是我的REST服務的數據合同。 字節數組屬性將接受最大大小限制為500MB的任何文檔。 當我嘗試使用此服務並序列化對象時,出現內存不足異常錯誤。 請找到下圖
下面是代碼片段
public static string SerializeJSon<T>(T t)
{
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
DataContractJsonSerializerSettings s = new DataContractJsonSerializerSettings();
DateTimeFormat dt = new DateTimeFormat("MM/dd/yyyy");
s.DateTimeFormat = dt;
**ds.WriteObject(stream, t);**
string jsonString = Encoding.UTF8.GetString(stream.ToArray());
stream.Close();
return jsonString;
}
嘗試“在Web服務調用中不要使用500mb的文檔”。 THs是核心問題-您嘗試使用一種方法調用機制來傳輸半GB的數據,這些數據可能會變成內存對象中的GB數。 這不是Web服務的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.