我在我的 .NET 5.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?)
MSBuild版本:Microsoft (R) Build引擎版本16.9.0+57a23d249,用于.NET
编辑
在Visual Studio开发人员PowerShell中运行msbuild -t:Rebuild
构建成功,但我无法在Gitlab CI/CD中使用。
项目结构:
-- Project.Api
/-- SourceGenerator.A
/-- Project.Domain
/-- SourceGenerator.B
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"]
Project.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>
Project.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>
msbuild -t:Rebuild
进行构建,以查看是否是生成器的问题,因为dotnet
cli 在 .NET 5 上运行生成器,而 VS 和 MSBuild 在 .NET Framework 上运行生成器。(但在这一点上只是猜测) - undefined