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

半世苍凉 提交于 2019-12-23 10:59:26

问题


B"H

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' 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.

Thank you


回答1:


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.



来源:https://stackoverflow.com/questions/46040113/valuetuplet1-t2-exists-in-both-system-valuetuple-and-mscorlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!