Rabbi
Rabbi

Reputation: 4722

'ValueTuple<T1, T2>' exists in both 'System.ValueTuple ...' and 'mscorlib ...'

I am trying to use the new Tuple features of C# 7, and running into a little bit of an issue. Actually, they were working fine, and I am not sure what changed to make them break.

I am working with an ASP.Net 4, MVC 5, targeting .net framework 4.6.1

So in order to use tuples I had to a the Nuget Package 'System.ValueTuple'. Without it, the project won't compile. It worked fine for a while. Then today when I load any page that uses Tuples I get

Compiler Error Message: CS0433: The type 'ValueTuple<T1, T2>' exists in both 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

The project compiles fine, and if I remove System.ValueTuple then it doesn't compile. But when I run it in a web page I get this error.

My searches on google suggest that ValueTuple was added to .net 4.7 but I have no reference to 4.7 anywhere in my project and I am not compiling against 4.7. My VS 2017 doesn't even have that option.

Upvotes: 8

Views: 4792

Answers (2)

rod0302
rod0302

Reputation: 121

I had this issue in my WPF application targeting .NET 4.7.2. My test project uses Specflow, and one of its dependencies is System.ValueTuple, so I couldn´t uninstall it. So, what I did instead was deleting the reference to the nuget package in the app.config file of the project.

Delete:

<dependentAssembly>
     <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
     <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
</dependentAssembly>

Upvotes: 0

user743382
user743382

Reputation:

ASP.NET web pages are compiled at runtime, and this compilation ignores the reference assemblies that VS uses. If .NET 4.7 is available at runtime, all of .NET 4.7's types, including its definition of ValueTuple, will be picked up along with any other definition you might have from other DLLs.

The easiest ways to avoid this problem if you've already installed .NET 4.7 are either to use .NET 4.7 in your development environment as well, or to avoid runtime compilation.

For the former, you'd need to install the .NET 4.7 targeting pack. It should be an option in the Visual Studio installer. When you do that, you can avoid the ValueTuple NuGet package and thereby the conflict.

For the latter, you can turn on precompilation when you publish your web project. When you do this, all the compilation that normally happens at runtime instead occurs during the publishing, in a controlled way where .NET 4.7's additions won't be seen.

Upvotes: 6

Related Questions