使用源生成器构建 C# dotnet

Dan*_*iel 6 .net c# asp.net msbuild

我的 .net5.0 项目中有源生成器。在 Visual Studio 中,一切顺利,但是当我尝试使用 dotnet cli 源生成器构建应用程序时,无法运行:

error CS1061: 'CentralSystemResponseSender' does not contain a definition for 'SendResponseAsync' and no accessible extension method 'SendResponseAsync' accepting a first argument of type 'CentralSystemResponseSender' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

MSBuild版本:Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET


编辑

msbuild -t:Rebuild在 Visual Studio 中,开发人员 powershell 构建成功,但我无法在 Gitlab CI/CD 中使用它

项目结构:

-- Project.Api
     /-- SourceGenerator.A
  /-- Project.Domain
     /-- SourceGenerator.B
Run Code Online (Sandbox Code Playgroud)

dotnet build "Project.Domain.csproj"成功
dotnet build "Project.Api.csproj"会抛出错误,因为SourceGenerator.B不生成代码。
构建整个解决方案也可以工作,但dotnet publish再次抛出错误,因为SourceGenerator.B不生成代码。

Dockerfile:


FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY . .
RUN dotnet restore "generators/SourceGenerator.A/SourceGenerator.A.csproj"
RUN dotnet restore "generators/SourceGenerator.B/SourceGenerator.B.csproj"
RUN dotnet restore "src/Project.Domain/Anaconda.Domain.csproj"
RUN dotnet restore "src/Project.Api/Project.Api.csproj"
RUN dotnet build -c Release -o /app/build

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.Api.dll"]
Run Code Online (Sandbox Code Playgroud)

项目.Api.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>..\..</DockerfileContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\generators\Project.Generator.A\Project.Generator.A.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\Project.Domain\Anaconda.Domain.csproj" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

项目.Domain.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\generators\Project.Generator.B\Project.Generator.B.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

Alb*_*ozi 1

希望这个答案能在两年后帮助其他人。

我们遇到了同样的问题,但正如 Vincent 在评论中所说,通过将“Microsoft.Net.Compilers.Toolset”添加到将源生成器项目引用为分析器的项目的.csproj中来解决。

    <ItemGroup>
        ...
        <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.8.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\DocumentationGenerator\DocumentationGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" TargetFramework="netstandard2.0" />
        ...
    <ItemGroup>
Run Code Online (Sandbox Code Playgroud)