c# - 服务器上的 MSBuild 错误CS0234 错误 .net 4.7.2

标签 c# .net msbuild visual-studio-2019 .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 Studio\2019\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 设置: get

有什么想法吗?

编辑:我添加了 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 的基本框架: enter image description here

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

最佳答案

1.您可以尝试删除这些软件包,然后重新安装它们。类似问题参见here 。 (删除软件包并重新安装可以避免类似packages.config中列出的所有软件包都已安装的情况。)

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

也许您应该引用 Newtonsoft.Json 包来解决错误无法找到类型或命名空间名称“Newtonsoft”。对于packages.config 格式,您应该同时使用packages.config和 xx.csproj 文件,所以我建议您使用 Nuget Manager UI 在 VS IDE 中执行此操作。

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

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

enter image description here

关于c# - 服务器上的 MSBuild 错误CS0234 错误 .net 4.7.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57716301/

相关文章:

.net - MSBuild ItemGroup,不包括 .svn 目录和文件

asp.net - 禁用 MSBuild TypeScript 编译

c# - 连接池

c# - CRM 2013 - 在 View 中排序记录

c# - View 模型应该更接近 View 还是模型?

c# - 升级到 .NET 4.0 并获取无法加载文件或程序集对已删除的旧版本 DLL 的引用

.net - 使用 WIX 时删除单元测试 dll 文件

c# - 工厂如何避免switch case

c# - 算术运算结果与字符串的连接

c# - 如何将两个数据表连接到另一个数据表