Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.OutOfMemoryException when merging Newtonsoft.Json

I am creating a plugin to call a webservice. I need to serialize and deserialize the Json object. So, I need Newtonsoft.Json. I am trying to merge the dll from NewtonSoft.Json and my application dll using ILMerge.MSBuild.Task and ILMerge in Visual Studio 2015.

I get the error below: enter image description here

I looked for solution in internet but could not find any solution.

like image 288
sabin Avatar asked Dec 03 '18 22:12

sabin


3 Answers

For ILMerge in VisualStudio Use the necessary dlls from NuGet Package Manager Only

I was using the MSBuild.ILMerge.Task 1.0.5 and latest verson of Newtonsoft.Json and getting this type of issue.

I tried with to stable version by downgrade to Newtonsoft.Json version 10.0.3 and it works well.

Hope this helps!!!

like image 88
Srikanta Mahapatro Avatar answered Oct 13 '22 13:10

Srikanta Mahapatro


If you're using ILMerge only to serialize/deserialize JSON I'd recommend to drop it and use the DataContractJsonSerializer class instead. This change would remove the dependency with Newtonsoft.Json and ILMerge (not supported) to end up with a lighter plugin library (which is always good):

// Deserialize a JSON stream to a User object.  
public static User ReadToObject(string json)  
{  
    User deserializedUser = new User();  
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  
    DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());  
    deserializedUser = ser.ReadObject(ms) as User;  
    ms.Close();  
    return deserializedUser;  
} 

Full example can be found here.

like image 20
Federico Jousset Avatar answered Oct 13 '22 14:10

Federico Jousset


I was able to fix this issue by taking the latest dll from nuget, & just putting it into side folder & reference the dll direct.

Im not sure why nuget messes it up, but after i took nuget out of the picture the the build worked.

I don't like the fact that i cant use nuget for getting updates for this project, but as least it workes.

like image 35
CMS Avatar answered Oct 13 '22 14:10

CMS