question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.IO.FileLoadException when trying to use Microsoft.Extensions.Configuration nuget package

See original GitHub issue

Stacktrace:

System.TypeInitializationException: The type initializer for 'Submission#0' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621) ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   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.AppDomain.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at Submission#0.<<Initialize>>d__0.MoveNext() in /Users/gldraphael/projects/misc/vi/config.csx:line 16
--- End of stack trace from previous location where exception was thrown ---
   at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in C:\Users\appveyor\AppData\Local\Temp\tmp382F\Dotnet.Script.Core\ScriptRunner.cs:line 58

Here’s the config.csx file I’m trying to run:

#!/usr/bin/env dotnet-script
#r "nuget: Microsoft.Extensions.Configuration, 2.2.0"
#r "nuget: Microsoft.Extensions.Configuration.FileExtensions, 2.2.0"
#r "nuget: Microsoft.Extensions.Configuration.Json, 2.2.0"
#r "nuget: Microsoft.Extensions.Configuration.Binder, 2.2.0"

using Microsoft.Extensions.Configuration;

public static var config = new ConfigurationBuilder()
          .AddJsonFile("config.json", optional: false, reloadOnChange: true)
          .AddJsonFile("config.secrets.json", optional: true, reloadOnChange: true)
          .Build()
          .Get<Config>();
Console.WriteLine(config.Secret); // works if I comment this out, but then what's the point

public class Config 
{
    public string Secret { get; set; }
}

$ dotnet script --info
Version             : 0.28.0
Install location    : /Users/gldraphael/.dotnet/tools/.store/dotnet-script/0.28.0/dotnet-script/0.28.0/tools/netcoreapp2.1/any
Target framework    : netcoreapp2.1
.NET Core version   : 2.1.5
Platform identifier : osx
Runtime identifier  : osx-x64

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
filipwcommented, Apr 3, 2019

dotnet-script has indirect dependency on Microsoft.Extensions.Configuration 2.1.1 which causes the issue, since that older version is already loaded into the runtime before you attempt to load yours from the script.

The most basic repro is:

(anything higher than 2.1.1 will break)

#r "nuget: Microsoft.Extensions.Configuration, 2.1.6"

using Microsoft.Extensions.Configuration;

var builder = typeof(ConfigurationBuilder);
Console.WriteLine(builder); 

We will try to come up with a fix, but for now, the recommended workaround is to use max. versions 2.1.1 of those config packages:

#!/usr/bin/env dotnet-script
#r "nuget: Microsoft.Extensions.Configuration, 2.1.1"
#r "nuget: Microsoft.Extensions.Configuration.FileExtensions, 2.1.1"
#r "nuget: Microsoft.Extensions.Configuration.Json, 2.1.1"
#r "nuget: Microsoft.Extensions.Configuration.Binder, 2.1.1"

using Microsoft.Extensions.Configuration;

public static var config = new ConfigurationBuilder()
          .AddJsonFile("config.json", optional: false, reloadOnChange: true)
          .AddJsonFile("config.secrets.json", optional: true, reloadOnChange: true)
          .Build()
          .Get<Config>();
Console.WriteLine(config.Secret); // should work now
public class Config 
{
    public string Secret { get; set; }
}
0reactions
drosenbacommented, Sep 25, 2020

Any solution here yet? I too am getting the same error when I use EPPlus.
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=3.1.5.0,

Happens when using only #r “nuget: EPPlus, 5.3.2”

and also when adding #r “nuget: Microsoft.Extensions.Configuration, 3.1.5”

Also happens if I copy the Microsoft.Extensions.Configuration.dll into the same folder as the .csx

(P.S. Works fine in LinqPad 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Could not load file or assembly Microsoft.Extensions. ...
I noticed in the error the Microsoft.Extensions.DependencyInjection.Abstractions 2.0.0 version was requested. Downgraded to that package in ...
Read more >
Could not load file or assembly Microsoft.Extensions. ...
I have a Visual Studio 2019 extension that references Microsoft.Extensions.Configuration v3.1.1. This assembly gets loaded by a code ...
Read more >
Upgrade my Azure Function from .net Core 3.1 to . ...
I have an Azure Function which run on .net core 3.1, but when i change the .net version from 3.1 to 7 i...
Read more >
[Solved] Microsoft.extensions.configuration could not load?
I need someone who can help to fix this issue, each time when i run my MVC application i get this exception "Microsoft...
Read more >
Azure Function and Entity Framework Sql Server error
I just ran into this exact same issue. To solve it, I left my function app project at .Net 6. Then with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found