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>