You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含netstandard2.0与4.7.2项目的解决方案dotnet build报类型不存在错误如何解决

问题根因

构建失败是由于传统非SDK风格的.NET Framework项目(MyService.csproj使用的是旧版csproj格式)对dotnet CLI的PackageReference支持不完善导致的:
Visual Studio 2019调用的是完整版MSBuild,可正确识别旧格式项目中的PackageReference配置、自动解析NuGet包依赖;但dotnet build原生仅对SDK风格项目的依赖解析做了完整兼容,处理旧格式.NET Framework项目时无法正确加载NuGet包引入的程序集,因此抛出类型不存在的错误。

解决方案(优先推荐):将MyService项目改为SDK风格csproj

直接替换MyService.csproj的全部内容为以下配置即可:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <OutputType>Library</OutputType>
    <RootNamespace>MyService</RootNamespace>
    <AssemblyName>MyService</AssemblyName>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Flurl.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
    <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
    <PackageReference Include="System.Text.Json" Version="5.0.2" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\AuthenticatorService\AuthenticatorService.csproj" />
  </ItemGroup>
</Project>

改完后执行以下命令即可正常构建:

# 先清理旧的构建缓存(Windows环境)
rd /s /q src\MyService\bin src\MyService\obj
dotnet restore MySolution.sln
dotnet build MySolution.sln
备选方案:保留旧项目格式,使用MSBuild构建

如果不想修改项目格式,可以直接使用Visual Studio配套的MSBuild命令完成构建,无需依赖dotnet CLI:

  1. 打开Visual Studio 2019的Developer Command Prompt
  2. 执行命令:
msbuild MySolution.sln -t:Restore,Build -p:Configuration=Release

内容的提问来源于stack exchange,提问作者Mark Nadig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:15:04