Using .NET Standard from .NET Framework web application fails with "Could not load file or assembly..."

1

I am trying to use .NET Standard libraries in .NET Framework ASP.NET web applications, and I frequently encounter errors relating to type loading. I have reproduced the problem as follows:

https://github.com/sgarshol/VTSample

I have built a bare-bones .NET Standard 2.0 library with a single class:

namespace VTLib
{
    public class VTMaker
    {
        public (int, string) GetVT()
        {
            return (3, "value");
        }
    }
}

I have referenced this library as a project reference in a console application and a ASP.NET MVC application, both .NET Framework v4.6.2. Both projects instantiate the class and call GetVT().

The console application works fine.

The web application does not, and I receive the following error:

Could not load file or assembly 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Checking my the web application/bin folder, I find that the following .dlls:

/bin/System.ValueTuple.dll (4.0.2.0, msil, .Net Framework v4.0) /bin/roslyn/System.ValueTuple.dll (4.0.1.0, msil, .Net Framework v.4.6.2)

Using JetBrains dotPeek, I examine my web application .dll and my library .dll and glean the following:

  • The web application .dll references System.ValueTuple (4.0.2.0), though the .csproj file includes no such reference.
  • The library .dll references netstandard (2.0.0.0) as its single reference.

I find that I can "resolve" the issue by manually adding an assembly redirect instruction in the Web.config file:

<dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
c#
asp.net
.net-standard
.net-framework-version
asked on Stack Overflow Nov 2, 2018 by Sigurd Garshol • edited Nov 2, 2018 by Paul Karam

1 Answer

0

You need to add the reference to your ASP.NET web project as well. Transitive references are not supported in this kind of project as described in this answer.

Make sure that all dependencies from referenced packages are also added as reference to your ASP.NET MVC application, including System.ValueTuple

answered on Stack Overflow Nov 4, 2019 by MikeLimaSierra

User contributions licensed under CC BY-SA 3.0