0

我有一个包含两个项目的解决方案,一个针对 4.7.2,一个针对 netstandard2.0。我可以在 Visual Studio 2019 中很好地构建解决方案。但是,当我运行dotnet build MySolution.sln它时,它会失败,并出现与该项目中引用的 nuget 包相关的一些“命名空间类型 ... 不存在”错误。

src\MyService\Authenticator.cs(2,17): error CS0234: The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [src\MyService\MyService.csproj]
src\MyService\Authenticator.cs(5,23): error CS0234: The type or namespace name 'Json' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?) [src\MyService\MyService.csproj]
src\MyService\Authenticator.cs(7,7): error CS0246: The type or namespace name 'Flurl' could not be found (are you missing a using directive or an assembly reference?) [src\MyService\MyService.csproj]

所有源代码都可以在https://github.com/marknadig/TestBuild获得。

第一个项目“MyService”是一个 .net 4.7.2 类库项目。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{3C5D1FFA-5620-4BE0-B79F-801DAD8B176A}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>MyService</RootNamespace>
    <AssemblyName>MyService</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
    <Compile Include="Authenticator.cs" />
</ItemGroup>
<ItemGroup>
    <PackageReference Include="Flurl.Http">
    <Version>3.2.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging">
    <Version>5.0.0</Version>
    </PackageReference>
    <PackageReference Include="System.Net.Http.Json">
    <Version>5.0.0</Version>
    </PackageReference>
    <PackageReference Include="System.Text.Json">
    <Version>5.0.2</Version>
    </PackageReference>
</ItemGroup>
<ItemGroup>
    <ProjectReference Include="..\AuthenticatorService\AuthenticatorService.csproj">
    <Project>{cce9bb31-d3e8-4573-8e3a-4ce0bf793fa1}</Project>
    <Name>AuthenticatorService</Name>
    </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

如您所见,MyService 项目引用了针对“netstandard20”的 AuthenticatorService 项目。

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFrameworks>netstandard2.0</TargetFrameworks>
    <AssemblyName>AuthenticatorService</AssemblyName>
    <RootNamespace>AuthenticatorService</RootNamespace>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
</ItemGroup>
</Project>

dotnet --version报告5.0.402

如何让 dotnet build 构建?

4

1 回答 1

0

结果是“dotnet cli 不支持旧式 csproj 文件,也不支持 windows 桌面项目,这就是它失败的原因。”

https://github.com/dotnet/sdk/issues/8931#issuecomment-350167706

这有效:

msbuild /t:restore TestBuild.sln
msbuild TestBuild.sln
于 2021-11-04T16:45:22.310 回答