FileLoadException when running C# script with .NET Core 3.1

802 views Asked by At

I wrote the following C# script (HelloWorld.csx file):

#! "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();

I'm using dotnet-script (version 1.0.1) and .NET Runtime (version 3.1.14)

When executing this script, I get the following error. Any idea why?

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

1

There are 1 answers

0
Francois C On BEST ANSWER

I created an issue on the dotnet-script repo and I got an answer from the dev. Refer to the issue for all the details.

The answer I got is that dotnet-script version 1.0.1 preloads version 4.7.1 of System.Text.Encoding.CodePages which means a newer major cannot be resolved.