🔷 Day 9: .NET Builds

Hands-On Guide - dotnet CLI, MSBuild & Azure DevOps Integration

📋 Day 9 Overview

Today's Learning Goals

  • ✅ Understand .NET platform and ecosystem
  • ✅ Install .NET SDK
  • ✅ Use dotnet CLI for builds
  • ✅ Understand .csproj project files
  • ✅ Manage NuGet packages
  • ✅ Integrate .NET with Azure Repos
  • ✅ Create basic Azure Pipeline for .NET

Key Concepts

Concept Description
.NET SDK Software Development Kit for building .NET apps
dotnet CLI Command-line interface for .NET operations
.csproj C# project file (like pom.xml)
NuGet Package manager for .NET (like Maven Central)
MSBuild Microsoft Build Engine (underlying build system)
💡 If you learned Maven (Day 8), .NET follows similar patterns!

⬇️ .NET SDK Installation

Download .NET SDK

Official Download:

Visit: https://dotnet.microsoft.com/download

Download: .NET 8.0 SDK (latest LTS)

Install - Windows

1 Download Installer

Download .NET 8.0 SDK installer (.exe)

2 Run Installer

Double-click installer, follow wizard

3 Verify Installation
dotnet --version 8.0.100 dotnet --info .NET SDK: Version: 8.0.100 Commit: abc123...

Install - macOS

# Using Homebrew brew install --cask dotnet-sdk # Verify dotnet --version # Or download from: https://dotnet.microsoft.com/download

Install - Linux (Ubuntu)

# Add Microsoft package repository wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb # Install .NET SDK sudo apt update sudo apt install -y dotnet-sdk-8.0 # Verify dotnet --version

🔷 dotnet CLI Commands

Project Creation

# List available templates dotnet new list # Create console application dotnet new console -n MyConsoleApp # Create Web API dotnet new webapi -n MyWebApi # Create MVC web app dotnet new mvc -n MyWebApp # Create class library dotnet new classlib -n MyLibrary # Create test project dotnet new xunit -n MyTests

Build Commands

# Restore NuGet packages dotnet restore # Build project (Debug mode - default) dotnet build # Build in Release mode dotnet build -c Release # Clean build artifacts dotnet clean # Run application dotnet run # Run tests dotnet test # Publish for deployment dotnet publish -c Release -o ./publish

Package Management (NuGet)

# Add package to project dotnet add package Newtonsoft.Json # Add specific version dotnet add package Microsoft.EntityFrameworkCore --version 8.0.0 # Remove package dotnet remove package Newtonsoft.Json # List installed packages dotnet list package # Update packages dotnet restore
dotnet CLI automatically updates .csproj when you add/remove packages!

📄 Understanding .csproj Files

What is .csproj?

C# Project file - defines project settings, dependencies, and build configuration

Minimal .csproj File

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> </PropertyGroup> </Project>

Complete .csproj with Dependencies

<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <!-- NuGet Package References --> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> </ItemGroup> </Project>

.csproj vs pom.xml Comparison

Aspect Maven (pom.xml) .NET (.csproj)
Project ID groupId + artifactId AssemblyName
Version <version> <Version>
Dependencies <dependencies> <ItemGroup> PackageReference
Build Config <build> <plugins> <PropertyGroup>

☁️ Azure DevOps Integration

Complete Workflow: .NET + Azure DevOps

1 Create .NET Project Locally
dotnet new webapi -n ProductAPI cd ProductAPI # Test build locally dotnet build Build succeeded.
2 Initialize Git Repository
# Create .gitignore for .NET dotnet new gitignore # Initialize Git git init git add . git commit -m "Initial .NET Web API project"
3 Create Azure Repos Repository
  1. Go to Azure DevOps: dev.azure.com
  2. Create new repository: "ProductAPI"
  3. Copy the Git clone URL
4 Push to Azure Repos
# Add Azure Repos as remote git remote add origin git@ssh.dev.azure.com:v3/org/project/ProductAPI # Push code git push -u origin main
5 Create Azure Pipeline

Create file: azure-pipelines.yml in project root

trigger: - main pool: vmImage: 'ubuntu-latest' steps: - task: UseDotNet@2 displayName: 'Install .NET SDK' inputs: version: '8.x' - task: DotNetCoreCLI@2 displayName: 'Restore packages' inputs: command: 'restore' - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' configuration: 'Release' - task: DotNetCoreCLI@2 displayName: 'Run tests' inputs: command: 'test' - task: DotNetCoreCLI@2 displayName: 'Publish' inputs: command: 'publish' publishWebProjects: false arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)' - task: PublishBuildArtifacts@1 inputs: pathToPublish: '$(Build.ArtifactStagingDirectory)' artifactName: 'drop'
Result: Every push to main triggers automatic build, test, and publish!

🔨 Practice Exercises

Exercise 1: Create First .NET App

📝 Task: Create and build a console application
# 1. Create console app dotnet new console -n HelloDotNet cd HelloDotNet # 2. View created files ls HelloDotNet.csproj Program.cs # 3. Run the app dotnet run Hello, World! # 4. Build the app dotnet build Build succeeded. # 5. Check output ls bin/Debug/net8.0/ HelloDotNet.dll HelloDotNet.exe

Exercise 2: Add NuGet Packages

📝 Task: Add and manage NuGet packages
# 1. Create project dotnet new console -n PackageDemo cd PackageDemo # 2. Add Newtonsoft.Json package dotnet add package Newtonsoft.Json info : PackageReference for package 'Newtonsoft.Json' added # 3. List packages dotnet list package # 4. View .csproj (auto-updated!) cat PackageDemo.csproj # 5. Restore and build dotnet restore dotnet build

Exercise 3: Web API Project

📝 Task: Create and run a Web API
# 1. Create Web API dotnet new webapi -n ProductAPI cd ProductAPI # 2. Run the API dotnet run Now listening on: https://localhost:7001 Now listening on: http://localhost:5000 # 3. Test in browser: https://localhost:7001/swagger # 4. Build for release dotnet build -c Release # 5. Publish dotnet publish -c Release -o ./publish

Exercise 4: Azure DevOps Integration

📝 Task: Push .NET project to Azure Repos
# 1. In your .NET project directory dotnet new gitignore # 2. Initialize Git git init git add . git commit -m "Initial commit" # 3. Create repo in Azure DevOps (web interface) # 4. Add remote and push git remote add origin YOUR_AZURE_REPOS_URL git push -u origin main # 5. Create azure-pipelines.yml # (Copy from hands-on guide) # 6. Commit and push pipeline git add azure-pipelines.yml git commit -m "Add pipeline" git push

Challenge: Complete .NET Solution

🎯 Challenge: Create multi-project solution
  1. Create solution file
  2. Add Web API project
  3. Add class library project
  4. Add test project
  5. Build entire solution
  6. Push to Azure Repos
dotnet new sln -n MyApp dotnet new webapi -n MyApp.API dotnet new classlib -n MyApp.Core dotnet new xunit -n MyApp.Tests dotnet sln add MyApp.API/MyApp.API.csproj dotnet sln add MyApp.Core/MyApp.Core.csproj dotnet sln add MyApp.Tests/MyApp.Tests.csproj dotnet build