首页 > 解决方案 > 引用 Newtonsoft.Json.dll 时在 mono 下运行时编译 C# 代码失败

问题描述

我们的软件是用C#running编写的.Net-Framework 4.7.2。我们的用户可以使用我们软件中内置的脚本编辑器来使用 C# 编写他们自己的用户脚本。为了实现这一点,我们正在使用 .Net 组件CodeDomProvider。为了能够以一种简单的方式使用 JSON,我们希望Newtonsoft.Json.dll在我们的用户脚本中引用。这是通过添加对 CodeDomProvider 的 CompilerParameters 的引用来完成的。通过使用最新的 Newtonsoft.JsonNuGet包 (12.0.3),将 Newtonsoft.Json 添加到我的应用程序中。

这是此设置的最小示例:

using System;
using System.CodeDom.Compiler;
using System.IO;

namespace ScriptingUnix
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string output;
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    output = "/tmp/MyUnixTest.dll";
                }
                else
                {
                    output = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyUnixTest.dll");
                }

                CompilerParameters parameters = new CompilerParameters
                {
                    GenerateInMemory = false,
                    IncludeDebugInformation = true,
                    OutputAssembly = output
                };

                using (CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp"))
                {
                    parameters.ReferencedAssemblies.Add("System.dll");
                    parameters.ReferencedAssemblies.Add("Newtonsoft.Json.dll");
                    CompilerResults results = codeProvider.CompileAssemblyFromSource
                    (
                        parameters, 
                        "using System;\nusing Newtonsoft.Json.Serialization;\nusing Newtonsoft.Json.Linq;\n class Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        \r\n    }\r\n}"
                    );

                    foreach (CompilerError compilerError in results.Errors)
                    {
                        Console.WriteLine(compilerError);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
        }
    }
}

此设置在 Windows 上的 .Net Framework 下运行良好。问题是,我们还提供了在 (Arch)-Linux 上运行 Mono 的软件版本(Linux 4.19.80-1-ARCH armv7l,Mono JIT 编译器版本 6.0.0 (makepkg/6256b82d62f Wed 25 Sep 2019 05 :04:08 AM UTC)),其中使用单声道运行相同的代码会导致error CS0006: Metadata file Newtonsoft.Json.dll could not be found.

我已经尝试将以下内容添加到我的应用程序的 app.conf 中,但这没有帮助。重新安装NuGet软件包也没有帮助。将 添加Newtonsoft.Json.dll到 GAC 不起作用(以 失败Failure adding assembly Newtonsoft.Json.dll to the cache: The file specified is not a valid assembly.)。在尝试使用 app.conf 时,我观察到错误消息更改为error CS0009: Metadata file /opt/Newtonsoft.Json.dll does not contain valid metadata: Metadata file /opt/Newtonsoft.Json.dll does not contain valid metadata. 这就是为什么我猜他找到了 dll 但需要额外的、有效的肉类数据。我想到了某种 dll.conf。

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

有没有人暗示如何让它发挥作用?

标签: c#json.netmono

解决方案


你有两种可能:

  1. 使用来自 github 的最新 Mono (6.6)。如果包不可用,请编译它。

  2. 将 Newtonsoft.Json 降级到版本 11.0。如果需要,请使用绑定重定向。


推荐阅读