c# - ASP.NET Web API 项目中的 System.Data.Common、System.Net.Http 框架与 Nuget 程序集

标签 c# msbuild nuget

我对这两个包含在 .NET Framework 中但在 Nuget 上也可用的库感到非常困惑。我的直觉是使用 Nuget 之一,因为我会得到更频繁的更新等。然而,Visual Studio 即使我已经引用了来自 nuget 的那个,msbuild 使用来自 .NET 框架的一个,位于:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib

我在 github 上找到的一种解决方法是从 msbuild 文件夹中删除这些库。但是每当我更新 VS Studio 时,它都会将它们带回那里。 nuget 包还引入了一些绑定(bind)重定向。我数不清有多少次构建顺利,然后第 X 次构建给出了一个错误,这对这些程序集毫无意义。

您是否建议删除 nuget 包并使用框架中包含的包?

最佳答案

我有一个 answer来自 Visual Studio 论坛的 Jose Perez Rodriguez 很好地解释了这一点:

Hello Jakub, and thanks for posting your question. Sorry it took us so long to respond. First of all, I do agree with you that this space in general is a bit confusing, but here is a TL;DR explanation of the issue. System.Net.Http and System.Data.Common packages to get updated more frequently, but only for the .NET Core implementation, not for .NET Framework which is what you are targetting. .NET Framework actually already has a version of these two assemblies installed in the GAC, but it is not the newest version (at least that is the case for frameworks < 4.7.2), and if your application has any dependency that is targeting .NET Standard 1.4+, then you will most likely need the very latest version of these assemblies in order for your app to work correctly at runtime. The latest version for .NET Framework is not the one in the GAC nor the one in the packages as I've said before, but instead the one found in the MSBuild Extensions folder that you found. Those versions are really the latest versions for .NET Framework, so when our build detects that you will need them, we inject them into your bin folder and we reference those in order to be able to not hit any runtime issues.

So all in all, the conclusion is that whichever workaround you found saying to delete those files from that directory is incorrect, and should never really be done as that may cause your app to have runtime issues. In order to let your app work properly, remove the binding redirects that were added automatically by NuGet when you installed the System.Net.Http and the Data.Common packages, and make sure you turn on AutoGenerateBindingRedirects. After taking those steps (and making sure that you restore that msbuild extensions folder since you said you deleted some files from it) your app in the end should have a copy of these assemblies in the bin folder with the latest version of them, and it should also have an app config with the right binding redirects generated at build time.

We will do a better job of explaining this behavior in github, since a lot of people are confused by this behavior. I hope this answers your question, but if not, please feel free to ask any follow up questions you may have.

关于c# - ASP.NET Web API 项目中的 System.Data.Common、System.Net.Http 框架与 Nuget 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874184/

相关文章:

nuget - 如何在packages.config 中指定来自外部源的NuGet 包?

c# - NuGet:在安装/更新/卸载时运行 C# 代码

c# - 在没有显式 ServicePointManager.SecurityProtocol 调用的情况下,.NET 4.7 中未协商 TLS 1.2

c# - FileHelpers - 错误转换 '"""ID""' to type: ' Int32'

c++ - Visual Studio 2008 项目的 msbuild

C# 孙项目 DLL 未包含在 msbuild 包中

MSBuild OutputPath 属性和绝对路径

asp.net - 为什么在尝试使用 NuGet 包构建解决方案时,Visual Studio 会拒绝访问,代码 5 错误?

c# - ElasticSearch创建索引

c# - 在映射中使用 Linq 查询