System.OutOfMemoryException when merging Newtonsoft.Json

左心房为你撑大大i 提交于 2020-01-24 04:35:12

问题


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:

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


回答1:


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!!!




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/53602936/system-outofmemoryexception-when-merging-newtonsoft-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!