.net - 使用 MSBuild 在 Server 2016 上构建面向 .Net 4.0 的 ASP.Net 项目

标签 .net windows msbuild .net-4.0 windows-server-2016

我在 Windows Server 2016 上设置了 Jenkins 构建服务器。我安装了“Visual Studio 2017 构建工具”(最新版本的 MSBuild)。当我尝试构建针对 .Net 4.0 的项目时,我收到以下错误消息:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1122,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.0" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

于是我检查了“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0”,发现这个文件夹中果然没有dll,只有xml文件。

因此,我尝试安装完整的 .Net 4.0 框架,认为将安装引用程序集,但我收到以下消息:

1) Microsoft .NET Framework 4 is already a part of this operating system. You do not need to install the .NET Framework 4 redistributable.

2) Same or higher version of .NET Framework 4 has already been installed on this computer.

所以我谷歌了一下,找到了几个网站,上面写着类似 '.NET Framework 4.5.2 is considered to be an "in-place update" to the .NET 4 family, with no need to recompile applications' 的内容。我想也许我可以安装 .Net 4.5.2 目标包并使用 MSBuild 的此参数来定位它们:

/property:FrameworkPathOverride="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2"

果然,这似乎有效。问题是我不完全理解引用程序集和目标包等等。我担心,虽然看起来我可能已经解决了这个问题,但稍后我会发现,在更远的地方,我引入了一些其他令人讨厌的问题。

问题

  • 我所做的事情有什么问题吗?
  • 有更好的方法吗?

更新

我没有提到我还尝试通过this link安装.Net Framework 4.0.3 milti-targeting包但我收到以下错误:

Microsoft .NET Framework 4 Multi-Targeting Pack was not found. Please repair your installation for Microsoft Visual Studio 2010 in order to get this update.

最佳答案

简短回答

我不建议您继续使用 FrameworkPathOverride。这会将您的应用程序更改为像 .NET 4.5.2 而不是 4.0 一样进行编译。将会产生意想不到的后果。

您可以在此处下载 .NET Framework 4.0 目标包:https://www.microsoft.com/en-us/download/details.aspx?id=29052

或者,您可以通过在“单独组件”中选择 Visual Studio 2017 安装程序来下载 .NET 4.0 目标包。

vs installer

更多详细信息

The problem is I don't fully understand reference assemblies and targeting packs and whatnot.

“引用程序集”基本上类似于 .NET 中的头文件。当您为 .NET Framework 4.0 编译应用程序时,您将“定位”.NET 4.0 运行时中可用的 API。这形成了您的应用程序可以运行的最低运行时版本。但是,应用程序将在计算机上安装的 .NET Framework 运行时版本(它是全局共享版本)上执行,该版本几乎肯定会比 4.0 更新。

安装目标包仅意味着您正在将“头文件”的副本安装到您​​的计算机上。您的计算机仍将安装 .NET Framework 4.5.x 或 4.6 运行时。

由于 .NET 4.0 不再受支持,并且大多数计算机都具有 4.5.2 或更高版本,因此您应该考虑针对 .NET Framework 4.5.2 进行编译。您可以从 Visual Studio 中的项目属性页面执行此操作。或者,您可以编辑 csproj 文件。

如果您使用“经典”、更详细的 csproj 格式,那就像这样

   <PropertyGroup>
      <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
   </PropertyGroup>

如果您使用 VS 2017 的"new"、更精简的 csproj,那就是

   <PropertyGroup>
      <TargetFramework>net452</TargetFramework>
   </PropertyGroup>

关于.net - 使用 MSBuild 在 Server 2016 上构建面向 .Net 4.0 的 ASP.Net 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746620/

相关文章:

c# - C#中构造OutOfMemoryException没有内存时会发生什么?

.net - 在 F# 中正确使用带有指针的 P/invoke

visual-studio - 在 Visual Studio 中构建完成后自动发布 ClickOnce

c# - WPF Clickonce 使用 Microsoft.Net.Sdk 发布

c# - ViewModels和UI

c# - 使用这个结构有什么问题吗?

c - 如何返回文件创建日期?

php exec() 挂起

Windows 按端口号杀死进程

.net - 如何在 Visual Studio 2017 中使用 BeforeBuild 和 AfterBuild 目标?