Could not load file or assembly 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies

5

Over the last few days, I have been desperately trying to resolve a case of dependency hell I got into after installing a NuGet package which added a bunch of new dependencies from the System.Collections.*, System.Diagnostics.*, System.IO.*, System.Runtime.*, System.Text.*, System.Threading.* and System.Xml.* namespaces. The MVC project I have the issue in targets .Net 4.7.1 currently.

The package installation adds the following binding redirects to my web.config

  <dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.IO" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Linq" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Xml.ReaderWriter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Linq.Expressions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>

…which results in the exception below:

Could not load file or assembly 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

The highlighted line in the web.config is:

<add assembly="System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

I eventually found out that if I removed the bindingRedirect for System.IO it cleared that error and then complained about one of the other items in the list of binding redirects. One by one I removed all of the binding redirects and the problem is resolved.

I'm struggling to understand what is going on here. As an example, if I take a look at the property inspector for System.IO in my references in the Visual Studio project it points to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\Facades\System.IO.dll and not the Nuget folder yet if I open the .csproj file in another editor I can see that the reference does, in fact, look as follows:

<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
  <Private>True</Private>
  <Private>True</Private>
</Reference>

Something is definitely wrong here because there are duplicate <Private>True</Private> entries, I've corrected this once but they are added back. This is an issue for all of the new assemblies. I wonder if there is an issue with the Nuget packages somewhere?

Any help/advice would be appreciated please.

c#
.net
visual-studio
web-config
nuget
asked on Stack Overflow Aug 21, 2018 by ProNotion

1 Answer

5

I wonder if there is an issue with the Nuget packages somewhere?

Yes, this should be the issue for the nuget package. Some nuget packages take a dependency on NETStandard.Library, it will pull-in those system.* dependencies, on the other hand, some nuget package install dependencies under the .NET Standard framework incorrectly, I have even reported it here and here, this issue is related to the nuget package itself, we need to contact to the author of this package to update this package.

Above may be the reason that you got into after installing a NuGet package which added a bunch of system.* dependencies. Meanwhile, .NET Framework 4.7.1 adds support for .NET Standard in-box, we no need to add those system.* nuget package to the package. When other nuget package pull in those system.* dependencies packages, nuget/VS will add binding redirect to the web.config file for those package from nuget, but VS use those system.* from .net framework by default, in this case, VS may not find the those system.* dependencies due to the binding redirect. That the reason why you got the error "Could not load file or assembly 'System.IO...".

To resolve this issue, the workaround is remove the binding redirects from the web.config file for the assemblies. Check the thread .NET Standard issues on .NET Framework 4.7.1 for some details.

Besides, If you do not want add those System dependencies to your project you can delete them from your project.

Hope this helps.

answered on Stack Overflow Aug 22, 2018 by Leo Liu-MSFT

User contributions licensed under CC BY-SA 3.0