使用 .NET Core 3.1 的 运行 C# 脚本时出现 FileLoadException
FileLoadException when running C# script with .NET Core 3.1
我编写了以下 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 (version 1.0.1) and .NET Runtime(版本 3.1.14)
执行此脚本时,出现以下错误。知道为什么吗?
dotnet-script HelloWorld.csx
System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621)
File name: 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
---> System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
at Submission#0.Script.Run()
at Submission#0.<>d__0.MoveNext() in C:\HelloWorld.csx:line 18
--- End of stack trace from previous location where exception was thrown ---
at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in /private/var/folders/6j/4hkjjhd50fg27s933nctz0480000gn/T/tmp3iI6tV/Dotnet.Script.Core/ScriptRunner.cs:line 52
我在 dotnet-script repo and I got an answer from the dev. Refer to the issue for all the details 上创建了一个问题。
我得到的答案是 dotnet-script 版本 1.0.1 预加载了 System.Text.Encoding.CodePages 的版本 4.7.1,这意味着无法解析较新的专业。
我编写了以下 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 (version 1.0.1) and .NET Runtime(版本 3.1.14)
执行此脚本时,出现以下错误。知道为什么吗?
dotnet-script HelloWorld.csx
System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621) File name: 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly) at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath) at System.Reflection.Assembly.LoadFrom(String assemblyFile) at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args) at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name) at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName) at Submission#0.Script.Run() at Submission#0.<>d__0.MoveNext() in C:\HelloWorld.csx:line 18 --- End of stack trace from previous location where exception was thrown --- at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in /private/var/folders/6j/4hkjjhd50fg27s933nctz0480000gn/T/tmp3iI6tV/Dotnet.Script.Core/ScriptRunner.cs:line 52
我在 dotnet-script repo and I got an answer from the dev. Refer to the issue for all the details 上创建了一个问题。
我得到的答案是 dotnet-script 版本 1.0.1 预加载了 System.Text.Encoding.CodePages 的版本 4.7.1,这意味着无法解析较新的专业。