Search code examples
azure-devopsazure-pipelinesazure-pipelines-yamlazure-repos

Azure Pipelines for .Net 7 Solution with Multiple Projects


I have a .NET 7 solution with multiple projects. In addition, the projects are referencing each other. For example, the API, Service, and Respository projects are referencing the Model project. Locally, I was able to build the solution without issues. I push the project to my azure repo and created a pipeline. I follow the instruction in this documentation and that one as well. Running the pipeline kept failing (see partial error). Also, I have another pipeline for a solution with a single project that is working as expected. I am thinking that I made a stupid mistake or overlook something simple.

Pipeline:

trigger:
  branches:
    include:
    - master
    - releases/*
  paths:
    include:
    - docs/*
    exclude:
    - docs/README.md

pool:
  name: 'CustomAgent'

variables:
  solution: '**/*.sln'
  project: '**/*.csproj'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1
  inputs:
    versionSpec: '6.3.1'
    checkLatest: true

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: DotNetCoreCLI@2
  inputs:
    command: build
    project: $(solution)
    publishWebProjects: false
    arguments: '--configuration $(buildConfiguration) --no-restore'
    displayName: 'dotnet build $(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

Partial Error message:

2023-04-08T14:44:24.9740549Z ##[section]Starting: dotnet build Release
2023-04-08T14:44:24.9855598Z ==============================================================================
2023-04-08T14:44:24.9855723Z Task         : .NET Core
2023-04-08T14:44:24.9855774Z Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
2023-04-08T14:44:24.9855868Z Version      : 2.217.0
2023-04-08T14:44:24.9855930Z Author       : Microsoft Corporation
2023-04-08T14:44:24.9855985Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
2023-04-08T14:44:24.9856071Z ==============================================================================
2023-04-08T14:44:25.8863094Z [command]C:\Windows\system32\chcp.com 65001
2023-04-08T14:44:25.8999569Z Active code page: 65001
2023-04-08T14:44:25.9120542Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
2023-04-08T14:44:25.9174307Z [command]"C:\Program Files\dotnet\dotnet.exe" build D:\a\1\s\Registration.csproj "-dl:CentralLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.217.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"*ForwardingLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.217.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"" --configuration Release
2023-04-08T14:44:26.4932404Z 
2023-04-08T14:44:26.4977677Z Welcome to .NET 7.0!
2023-04-08T14:44:26.4978185Z ---------------------
2023-04-08T14:44:26.4978370Z SDK Version: 7.0.202
2023-04-08T14:44:26.4978463Z 
2023-04-08T14:44:26.4979412Z Telemetry
2023-04-08T14:44:26.4979746Z ---------
2023-04-08T14:44:26.4980215Z The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.
2023-04-08T14:44:26.4980450Z 
2023-04-08T14:44:26.4980672Z Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry
2023-04-08T14:44:26.4980754Z 
2023-04-08T14:44:26.4980879Z ----------------
2023-04-08T14:44:26.4981009Z Installed an ASP.NET Core HTTPS development certificate.
2023-04-08T14:44:26.4981253Z To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
2023-04-08T14:44:26.4981474Z Learn about HTTPS: https://aka.ms/dotnet-https
2023-04-08T14:44:26.4981633Z ----------------
2023-04-08T14:44:26.4981822Z Write your first app: https://aka.ms/dotnet-hello-world
2023-04-08T14:44:26.4982029Z Find out what's new: https://aka.ms/dotnet-whats-new
2023-04-08T14:44:26.4982197Z Explore documentation: https://aka.ms/dotnet-docs
2023-04-08T14:44:26.4982341Z Report issues and find source on GitHub: https://github.com/dotnet/core
2023-04-08T14:44:26.4982602Z Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
2023-04-08T14:44:26.4982861Z --------------------------------------------------------------------------------------
2023-04-08T14:44:27.0198594Z MSBuild version 17.5.0+6f08c67f3 for .NET
2023-04-08T14:44:32.8193712Z   Determining projects to restore...
2023-04-08T14:44:32.8264608Z   Skipping project "D:\a\1\Registration.Model\Registration.Model.csproj" because it was not found.
2023-04-08T14:44:32.8265234Z   Skipping project "D:\a\1\Registration.Services\Registration.Services.csproj" because it was not found.
2023-04-08T14:44:32.8265583Z   Skipping project "D:\a\1\Registration.Sql\Registration.Sql.csproj" because it was not found.
2023-04-08T14:44:32.8292225Z   Skipping project "D:\a\1\Registration.Model\Registration.Model.csproj" because it was not found.
2023-04-08T14:44:32.8292713Z   Skipping project "D:\a\1\Registration.Services\Registration.Services.csproj" because it was not found.
2023-04-08T14:44:32.8294921Z   Skipping project "D:\a\1\Registration.Sql\Registration.Sql.csproj" because it was not found.
2023-04-08T14:46:33.7436623Z   Restored D:\a\1\s\Registration.csproj (in 2.01 min).
2023-04-08T14:46:34.3117714Z ##[warning]C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): Warning : The referenced project '..\Registration.Model\Registration.Model.csproj' does not exist.
2023-04-08T14:46:34.3127035Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Model\Registration.Model.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:34.3129305Z ##[warning]C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): Warning : The referenced project '..\Registration.Services\Registration.Services.csproj' does not exist.
2023-04-08T14:46:34.3130076Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Services\Registration.Services.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:34.3131568Z ##[warning]C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): Warning : The referenced project '..\Registration.Sql\Registration.Sql.csproj' does not exist.
2023-04-08T14:46:34.3132258Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Sql\Registration.Sql.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7182549Z ##[error]Program.cs(4,20): Error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?)
2023-04-08T14:46:37.7184683Z D:\a\1\s\Program.cs(4,20): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
-04-08T14:46:37.7219900Z D:\a\1\s\Controllers\RoleController.cs(9,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7222080Z ##[error]Controllers\RoleController.cs(10,24): Error CS0234: The type or namespace name 'Services' does not exist in the namespace 'Registration' (are you missing an assembly reference?)
23-04-08T14:46:37.7227073Z D:\a\1\s\Controllers\RoleController.cs(11,24): error CS0234: The type or namespace name 'Sql' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7229229Z ##[error]Controllers\UserController.cs(6,24): Error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?)
8T14:46:37.7448585Z 
2023-04-08T14:46:37.7448678Z Build FAILED.
2023-04-08T14:46:37.7448856Z 
2023-04-08T14:46:37.7449497Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Model\Registration.Model.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7450271Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Services\Registration.Services.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7451178Z C:\Program Files\dotnet\sdk\7.0.202\Microsoft.Common.CurrentVersion.targets(2116,5): warning : The referenced project '..\Registration.Sql\Registration.Sql.csproj' does not exist. [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7451920Z D:\a\1\s\Program.cs(4,20): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7452647Z D:\a\1\s\Program.cs(6,20): error CS0234: The type or namespace name 'Sql' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7453459Z D:\a\1\s\Program.cs(7,20): error CS0234: The type or namespace name 'Sql' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7454211Z D:\a\1\s\ExceptionMiddleWare.cs(3,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7454691Z D:\a\1\s\Controllers\AccountController.cs(12,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7455176Z D:\a\1\s\Controllers\RoleController.cs(6,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7455639Z D:\a\1\s\Controllers\RoleController.cs(7,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7456118Z D:\a\1\s\Controllers\RoleController.cs(8,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7456572Z D:\a\1\s\Controllers\RoleController.cs(9,24): error CS0234: The type or namespace name 'Model' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7457053Z D:\a\1\s\Controllers\RoleController.cs(10,24): error CS0234: The type or namespace name 'Services' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7457518Z D:\a\1\s\Controllers\RoleController.cs(11,24): error CS0234: The type or namespace name 'Sql' does not exist in the namespace 'Registration' (are you missing an assembly reference?) [D:\a\1\s\Registration.csproj]
T14:46:37.7503187Z D:\a\1\s\Helper\JwtUtils.cs(18,25): error CS0246: The type or namespace name 'IKeyVaultManager' could not be found (are you missing a using directive or an assembly reference?) [D:\a\1\s\Registration.csproj]
2023-04-08T14:46:37.7503410Z     3 Warning(s)
2023-04-08T14:46:37.7503505Z     77 Error(s)
2023-04-08T14:46:37.7503549Z 
2023-04-08T14:46:37.7503677Z Time Elapsed 00:02:10.47
2023-04-08T14:46:37.7848000Z ##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
2023-04-08T14:46:37.7849528Z ##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
2023-04-08T14:46:37.7882782Z Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
2023-04-08T14:46:37.7885336Z ##[error]Dotnet command failed with non-zero exit code on the following projects : [ 'D:\\a\\1\\s\\Registration.csproj' ]
2023-04-08T14:46:37.8016584Z ##[section]Finishing: dotnet build Release

Project File:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>disable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>de9427ed-6926-459b-bfca-dae39fbb25ee</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.8.2" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
    <PackageReference Include="Microsoft.Azure.Management.Graph.RBAC.Fluent" Version="1.38.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.5" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Registration.Model\Registration.Model.csproj" />
    <ProjectReference Include="..\Registration.Services\Registration.Services.csproj" />
<ProjectReference Include="..\Registration.Sql\Registration.Sql.csproj" />

Solution

  • verify if you have more than one .sln file in that repo, in whatever nested folder, because you are building all sln files in whatever folder with this: **/*.sln

    Building a single .sln file will respect the build order of the projects inside the .sln in terms of project reference:

    Solution1.sln contains A.csproj and B.csproj, A project references B project, then building Solution1.sln will first build B then build A.

    But if in your repo there are other .sln files I am not sure how that would work out. In your command you are using glob matching to build all solutions in whatever nested foder the .sln file is (**/*.sln) so imagine this:

    Solution1.sln contains A.csproj and B.csproj, A project references B project

    Solution2.sln contains B.proj

    If Solution1 is built then B proj is built, after that A proj is built, but if just before A starts building Solution2 gets built then B proj is built again but from the other solution, if that happens just before A project gets built, then an error like that can happen.

    I am not sure if the dotnet cli Task processes all solutions it finds via **/*.sln in parallel or serial. I personally would reference directly the project that I am aiming to build, the references will be automatically restored and built as well, so i would use ./path/to/myMainBinary.csproj and not even perform the dotnet restore before, as it will be automatically called if you don't suppress that with some parameters, but you are doing specifically that so you might have your business reasons.

    If for any other reason you prefer or need using .sln files then i would reference directly the sln file you want to build ./path/to/Specific.sln