问题
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