2

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>
Paul Karam
  • 4,052
  • 8
  • 30
  • 53
Sigurd Garshol
  • 1,376
  • 3
  • 15
  • 36
  • Have you used Nuget Package Manager to bring System.ValueTuple into your project? – IrishChieftain Nov 02 '18 at 12:31
  • @IrishChieftain Adding System.ValueTuple to the web application references does not have any appreciable effect on the problem: The relevant .dlls output to the /bin folders remain the same; the error persists. – Sigurd Garshol Nov 02 '18 at 13:21
  • Reason I asked was because I saw no mention of it in the packages file, so I thought it might be worth a try doing this explicitly ;-) – IrishChieftain Nov 02 '18 at 13:58
  • @irishchieftain I avoided adding the package to keep the project to a minimum: it builds fine without it. But I did do a test with the package included (no dice!) at your suggestion, so your input is appreciated :) – Sigurd Garshol Nov 02 '18 at 17:53
  • We've had something very similar just now at work. I think there is a bug with the automatic binding redirect generation by .net framework 46x projects referencing .net standard libraries which fail to resolve the dependencies properly. For reference, 4.0.2 = nuget 4.4 and 4.0.3 = nuget 4.5. Why this happens is because you are using a C# 7.0 feature which only exists in .net framework 4.7+, so in order to use it in .net 462 you need to pull in an extra dll. I believe this is why it gets confused and doesnt go through the normal nuget dependency resolution process. – Michael Parker Mar 28 '19 at 15:37

1 Answers1

1

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

MikeLimaSierra
  • 799
  • 2
  • 11
  • 29