服务器上的 MSBuild errorCS0234 错误 .net 4.7.2

MSBuild on Server errorCS0234 errors .net 4.7.2

我正在尝试在我们的 tfs 服务器上构建一个新的解决方案,我遇到了很多错误,这里有几个例子:

App_Start\BundleConfig.cs (1,18): errorCS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) App_Start\FilterConfig.cs (1,18): errorCS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) App_Start\RouteConfig.cs (1,18): errorCS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) App_Start\WebApiConfig.cs (1,7): errorCS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) App_Start\WebApiConfig.cs (2,18): errorCS0234: The type or namespace name 'Http' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

为了解决问题,我使用的是 MSBuild:C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\msbuild.exe

解决方案设置为在.net 4.7.2中编译。

如果我 运行 在本地使用 MSBuild 命令行,它构建得很好,如果在服务器上失败并出现上述错误。我不确定我错过了什么。它还在 visual studio 2019 本地构建良好。

我已经检查了我的引用,这些引用是任何 nuget 包的一部分,它们指向包位置中的 dll。即

C:\somepath\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll

这是我的 nuget 设置:

有什么想法吗?

编辑:我添加了 build.proj 文件来恢复 nuget 包:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
         DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutDir Condition=" '$(OutDir)'=='' ">$(MSBuildThisFileDirectory)bin\</OutDir>
    <Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
    <SourceHome Condition=" '$(SourceHome)'=='' ">$(MSBuildThisFileDirectory)src\</SourceHome>
    <ToolsHome Condition=" '$(ToolsHome)'=='' ">$(MSBuildThisFileDirectory)tools\</ToolsHome>
  </PropertyGroup>
  <ItemGroup>
    <Solution Include="DCOAPI.sln">
      <AdditionalProperties>OutDir=$(OutDir);Configuration=$(Configuration)</AdditionalProperties>
    </Solution>
  </ItemGroup>
  <Target Name="RestorePackages">
    <Exec Command="&quot;NuGet.exe&quot; restore &quot;%(Solution.Identity)&quot;" />
  </Target>
  <Target Name="Clean">
    <MSBuild Targets="Clean"
             Projects="@(Solution)" />
  </Target>
  <Target Name="Build" DependsOnTargets="RestorePackages">
    <MSBuild Targets="Build"
             Projects="@(Solution)" />
  </Target>
  <Target Name="Rebuild" DependsOnTargets="RestorePackages">
    <MSBuild Targets="Rebuild"
             Projects="@(Solution)" />
  </Target>
</Project>

我收到这条消息:

All packages listed in packages.config are already installed.

当通过 MSBuild 和我的两个项目构建时,web api v2 proj 没有构建,我得到了上面的所有错误。

这些是相关的包:

<packages>
      <package id="Antlr" version="3.5.0.2" targetFramework="net472" />
      <package id="bootstrap" version="3.4.1" targetFramework="net472" />
      <package id="jQuery" version="3.3.1" targetFramework="net472" />
      <package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebApi" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net472" />
      <package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net472" />
      <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net472" />
      <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net472" />
      <package id="Modernizr" version="2.8.3" targetFramework="net472" />
      <package id="Newtonsoft.Json" version="11.0.1" targetFramework="net472" />
      <package id="WebGrease" version="1.6.0" targetFramework="net472" />
    </packages>

我添加到服务器的唯一东西是 VS2019 裸机:

我还需要安装其他东西吗?

1.You可以尝试删除那些包,然后重新安装。类似问题见 here。 (删包重装可以避免类似All packages listed in packages.config are already installed.的情况)

2.As 你上面提到的,它是一个 web api v2 项目。我创建了一个,发现它应该并且会自动添加对 Newtonsoft.Json 包的引用,但您的 packages.config 似乎不包含此引用。您是否修改了 packages.config 文件?

也许您应该参考 Newtonsoft.Json 包来解决错误 The type or namespace name 'Newtonsoft' could not be found。对于 packages.config 格式,您应该同时使用 packages.config 和 xx.csproj文件,所以我建议您使用 Nuget Manager UI.

在 VS IDE 中执行此操作

3.Also,如果包安装成功但仍然报错。您可能需要检查引发 CS0234 错误消息的 xx.csproj 文件中的内容。检查他们的提示路径是否正确。

对于 msbuild 的配置,您需要确保您的服务器使用的 VS 已经安装了 asp.net 和 Web 开发工作负载。

我必须将我使用的 Nuget.exe 从 2.8.6 更新到最新的 5.1,这样它才能构建 newtonsoft dll。

然后我不得不手动将包从本地复制到服务器(不知道为什么它们没有被自动添加):

C:\Program Files (x86)\Microsoft SDKs\NuGetPackages

此外,正如 LanceLiMSFT 所提到的,我必须更新 VS 以包含 asp.net 和 Web 开发工作量。

完成所有这些之后,我能够使用 MSBuild 通过命令行构建解决方案。

我仍然无法让 CCNet 构建解决方案。

编辑: 我可能需要在 CCNet 配置中添加一个步骤,以便在构建之前删除 packages 文件夹。无论出于何种原因,这似乎是关键,当通过 MSBuild

构建时

编辑:同样根据 LanceLiMSFT 的建议,删除构建服务器上的包允许 MSBuild 命令工作。

编辑:能够通过更新 build.proj 文件来构建 CCNet:

  <Target Name="RemoveDirectories">
    <Delete Files="C:\pathtopackages\packages\**\*" />
    <RemoveDir Directories="C:\pathtopackages\packages\" />
  </Target>

  <Target Name="RestorePackages" DependsOnTargets="RemoveDirectories">
    <Exec Command="&quot;NuGet.exe&quot; restore &quot;%(Solution.Identity)&quot;" />
  </Target>