微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 .NET Core 3.1 运行 C# 脚本时出现 FileLoadException

如何解决使用 .NET Core 3.1 运行 C# 脚本时出现 FileLoadException

我编写了以下 C# 脚本(HelloWorld.csx 文件):

#! "netcoreapp3.1"
#r "nuget: System.Text.Encoding.CodePages,5.0.0"

public class Script
{
    public static void Run()
    {
        try
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}

Script.Run();

我使用的是 dotnet-script(1.0.1 版)和 .NET Runtime(3.1.14 版)

执行此脚本时,出现以下错误。知道为什么吗?

dotnet-script HelloWorld.csx

System.IO.FileLoadException: 无法加载文件或程序集“System.Text.Encoding.CodePages,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”。无法找到或加载特定文件。 (0x80131621) 文件名:'System.Text.Encoding.CodePages,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileLoadException: 无法加载文件或程序集“System.Text.Encoding.CodePages,PublicKeyToken=b03f5f7f11d50a3a”。 在 System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext,String ilPath,String niPath,ObjectHandleOnStack retAssembly) 在 System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath) 在 System.Reflection.Assembly.LoadFrom(String assemblyFile) 在 System.Reflection.Assembly.LoadFromresolveHandler(Object sender,ResolveEventArgs args) 在 System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler,RuntimeAssembly assembly,String name) 在 System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly,String assemblyFullName) 在提交#0.Script.Run() 在 C:\HelloWorld.csx:line 18 中的 Submission#0.d__0.MoveNext() --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath,IEnumerable`1 commandLineArgs) 中的 /private/var/folders/6j/4hkjjhd50fg27s933nctz0480000gn/T/tmp3iI6tV/Script.Script2s/Core:

解决方法

我在 dotnet-script repo 上创建了一个问题,我从开发人员那里得到了答复。 Refer to the issue for all the details

我得到的答案是 dotnet-script 1.0.1 版预加载了 System.Text.Encoding.CodePages 的 4.7.1 版,这意味着无法解析较新的专业。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。