【问题标题】:System.OutOfMemoryException when merging Newtonsoft.Json合并 Newtonsoft.Json 时出现 System.OutOfMemoryException
【发布时间】:2018-12-03 22:29:06
【问题描述】:

我正在创建一个插件来调用网络服务。我需要序列化和反序列化 Json 对象。所以,我需要 Newtonsoft.Json。我正在尝试使用 Visual Studio 2015 中的 ILMerge.MSBuild.Task 和 ILMerge 将 NewtonSoft.Json 中的 dll 和我的应用程序 dll 合并。

我收到以下错误:

我在互联网上寻找解决方案,但找不到任何解决方案。

【问题讨论】:

标签: plugins json.net dynamics-crm dynamics-365 ilmerge


【解决方案1】:

对于 VisualStudio 中的 ILMerge,仅使用 NuGet 包管理器中的必要 dll

我正在使用 MSBuild.ILMerge.Task 1.0.5 和最新版本的 Newtonsoft.Json 并遇到此类问题。

我尝试通过降级到 Newtonsoft.Json 版本 10.0.3 来稳定版本,它运行良好。

希望对你有帮助!!!

【讨论】:

    【解决方案2】:

    如果您仅使用 ILMerge 来序列化/反序列化 JSON,我建议您放弃它并改用 DataContractJsonSerializer 类。此更改将删除与 Newtonsoft.Json 和 ILMerge (not supported) 的依赖关系,最终得到一个更轻量级的插件库(这总是好的):

    // 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;  
    } 
    

    完整的例子可以在here找到。

    【讨论】:

      【解决方案3】:

      我能够通过从 nuget 获取最新的 dll 来解决此问题,只需将其放入侧文件夹并直接引用 dll。

      我不确定为什么 nuget 会搞砸,但是在我将 nuget 从图片中取出后,构建工作正常。

      我不喜欢我不能使用 nuget 来获取这个项目的更新,但至少它可以工作。

      【讨论】:

        猜你喜欢
        • 2018-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        • 2011-04-24
        • 1970-01-01
        • 2013-05-30
        相关资源
        最近更新 更多