2

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

Francois C
  • 1,274
  • 2
  • 12
  • 14
  • Just tried replicating this but it worked for me. I installed dotnet-script as a global tool, created a csx file as shown above and then executed with `dotnet-script code-pages.csx` - no errors. Not helpful I know, but wonder if it is a permissions issue with wherever dotnet script is caching nuget packages? – phil_rawlings Apr 17 '21 at 16:23
  • @phil_rawlings Which runtime are you using? You can know which runtimes are installed by running this command: dotnet --list-runtimes – Francois C Apr 19 '21 at 13:01
  • 2.1.26, 2.127, 3.0.0-preview8-28405-07, 3.1.13, 3.1.14, 5.0.4 and 5.0.5 – phil_rawlings Apr 20 '21 at 13:44

1 Answers1

1

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.

Francois C
  • 1,274
  • 2
  • 12
  • 14