Command-Lines
To make it easier to enter commands at the prompt, this page lists all commands as a single line that can be copied and pasted.
- Chapter 1 - Hello, C#! Welcome, .NET!
- Chapter 2 - Making the Most of Your Code Editor
- Chapter 3 - Source Code Management Using Git
- Page 97 - Downloading the latest Git
- Page 98 - Configuring your Git identity
- Page 99 - Configuring SSH signature enforcement
- Page 101 - Configuring your default branch
- Page 101 - Getting help for Git
- Page 102 - Starting with a Git repository
- Page 103 - Creating and adding files to a Git repository in theory
- Page 104 - Creating a Git repository in practice
- Page 105 - Creating a new project
- Page 110 - Committing files
- Page 112 - Undoing a commit
- Page 113 - Cleaning a commit
- Page 115 - Ignoring files
- Page 118 - Viewing differences in files
- Page 124 - Filtering log output
- Page 125 - Managing remote repositories
- Page 130 - Walking through a branching and merging example
- Page 134 - Deleting and listing branches
- Chapter 5 - Logging, Tracing, and Metrics for Observability
- Chapter 6 - Documenting Your Code, APIs, and Services
- Chapter 9 - Building an LLM-Based Chat Service
- Chapter 11 - Unit Testing and Mocking
- Chapter 12 - Integration and Security Testing
- Chapter 13 - Benchmarking Performance, Load, and Stress Testing
- Chapter 14 - Functional and End-to-End Testing of Websites and Services
- Chapter 15 - Containerization Using Docker
code --install-extension ms-dotnettools.csdevkit
Install EF Core tools:
dotnet tool install --global dotnet-ef
Update EF Core tools:
dotnet tool update --global dotnet-ef
Generate entity class models for all tables:
dotnet ef dbcontext scaffold "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer --namespace Northwind.EntityModels --data-annotations
Add a new Console App / console
project named SnippetDemos4Code
to the Chapter02
solution
dotnet new console -o SnippetDemos4Code
dotnet sln add SnippetDemos4Code
Build and publish the self-contained release version of the console application for Windows, macOS and Linux:
dotnet publish -c Release -r win-x64 --self-contained
dotnet publish -c Release -r osx-x64 --self-contained
dotnet publish -c Release -r linux-x64 --self-contained
Install the project template for creating project templates:
dotnet new install Microsoft.TemplateEngine.Authoring.Templates
Package the project:
dotnet pack
Install the NuGet package:
dotnet new install .\bin\Release\ConsolePlusTemplate.1.0.0.nupkg
Confirm the current version of Git:
git --version
Set your name globally:
git config --global user.name "<your_full_name>"
git config --global user.email <your_email>
Set your name in a specific repository:
git config user.name "<your_full_name>"
git config user.email <your_email>
Generate an SSH key pair:
ssh-keygen -t ed25519 -C "[email protected]"
Configure Git to use your SSH key for signing commits:
git config --global user.signingkey <your-ssh-public-key-id>
git config --global commit.gpgformat ssh
Sign commits using the -S
switch:
git commit -S -m "your commit message"
Verify the signatures:
git log --show-signature
Check your default configuration:
git config --list --show-origin
Change the default branch to something less culturally insensitive, like main
:
git config --global init.defaultBranch main
git help <command>
git <command> --help
git <command> -h
To clone an existing repository into the current directory:
git clone <url_to_repo>
To initialize a repository in the current local non-Git directory:
git init
To add all C# files (files with the extension .cs
) and add a file named readme.md
:
git add *.cs
git add readme.md
After adding files, you must commit them to the local repository, preferably with a message specified using the -m switch:
git commit -m 'Initial version'
Get the current Git status of the current directory:
git status
Create a new console app in a subfolder named RepoDemo
:
dotnet new console -o RepoDemo
Create a solution and add the RepoDemo
project to it:
dotnet new sln
dotnet sln add RepoDemo
Stage the RepoDemo folder and all its files:
git add RepoDemo
Unstage all the files recursively (using the -r
switch):
git rm --cached RepoDemo -r
Stage just the two important source code files in the RepoDemo
folder and the solution file:
git add RepoDemo/Program.cs
git add RepoDemo/RepoDemo.csproj
git add Chapter03.sln
Get the current Git status of the current directory using the short switch, -s
:
git status -s
Commit the two files with a message:
git commit -m "Initial version"
Soft Reset:
git reset --soft HEAD
Mixed Reset:
git reset HEAD
Hard Reset:
git reset --hard HEAD
Revert Commit:
git revert <commit_hash>
Amend Commit:
git commit --amend
Remove files that should not be tracked by Git:
git clean
Remove files and directories that should not be tracked by Git:
git clean -d
Dry run option:
git clean -n
Create a .gitignore
file using the .NET SDK CLI:
dotnet new gitignore
Add and then commit the Git ignore file:
git add .gitignore
git commit -m "Add Git ignore file.
Show Git differences:
git diff
Show what differences are currently staged:
git diff --staged
Show the commit history:
git log
Show the commit history with the most recent three patches:
git log -p -3
Show the commit history statistics:
git log --stat
Show the commit history with one-line formatting:
git log --pretty=oneline
Show the commit history with a custom format that shows the full commit hash (%H), the author’s name (%an) in blue (%Cblue, %Creset), the author timestamp in ISO 8601 format (%ai), and then on a new line (%n), the commit message (%s):
git log --pretty=format:"Author of %H was %Cblue%an%Creset, at %ai%nMessage: %s%n"
Show the commit history with a custom format that shows the short commit hash (%h) and the commit message (%s), but with graphs for any branches:
git log --pretty=format:"%h %s" --graph
Get the commits in the last two weeks:
git log --since=2.weeks
git log --after=2.weeks
Get the commits starting in 2024:
git log --since=2024-01-01
git log --after=2024-01-01
Get the commits before 2024:
git log --until=2024-01-01
git log --before=2024-01-01
Filter by author, committer, or file path:
git log --author "Mark J Price"
git log --committer "Mark J Price"
git log -- RepoDemo/RepoDemo.csproj
Get the commits that contain the text string ignore
:
git log -S "ignore"
List your configured remote repositories:
git remote -v
To get more information:
git remote show origin
Add your remote repository with its URL:
git remote add origin https://github.com/markjprice/repodemo.git
Push the main branch of your project up to the remote repositories:
git push origin main
Stage the modified file and then commit the changes to both files with a comment message:
git commit -a -m "Add calculator functionality and call the Add method."
Create a new feature branch named configure-console
:
git branch configure-console
Switch to the new branch:
git switch configure-console
Create (-c
) a new feature branch and switch to it:
git switch -c <branch-name>
Create a new feature branch and switch to it (old way):
git checkout -b <branch-name>
Swtich (back) to main
branch:
git switch main
Merge the hotfix into the main branch:
git merge calc-hotfix
Delete the hotfix branch:
git branch -d calc-hotfix
List branches:
git branch
Install the .NET Counters tool globally:
dotnet tool update -g dotnet-counters
Start monitoring the web service by specifying its PID:
dotnet-counters monitor -p 9552 --counters Northwind.WebApi.Metrics
Install DocFX:
dotnet tool install -g docfx
Or update it to the latest version:
dotnet tool update -g docfx
Confirm that DocFX is installed:
dotnet tool list -g
Initialize a DocFX project:
docfx init
Host the DocFX project in a web server:
docfx docfx.json --serve
Install Mermaid CLI using Node Package Manager:
npm install -g @mermaid-js/mermaid-cli
mmdc -i mermaid-examples.md -o output.md
To quickly download and run an Ollama model in interactive mode:
ollama run <model>
Check its version:
ollama --version
Pull down a named model like Llama3:
ollama pull llama3
List the available local models:
ollama list
Run a named model (which would also download it if not already pulled):
ollama run llama3
Add a new xUnit Test Project [C#] / xunit project named BusinessLogicUnitTests
to the Chapter11
solution:
dotnet new xunit -o BusinessLogicUnitTests
dotnet sln add BusinessLogicUnitTests
Run the tests:
dotnet test --logger "console;verbosity=detailed"
Create a new migration:
dotnet ef migrations add <MigrationName>
Run any outstanding migrations:
dotnet ef database update
To revert to a specified migration point:
dotnet ef database update <MigrationName>
To revert all migrations so that the database returns to its original state:
dotnet ef database update 0
On Windows, use winget:
winget install Microsoft.devtunnel
On MacOS, use Homebrew:
brew install --cask devtunnel
On Linux, use curl:
curl -sL https://aka.ms/DevTunnelCliInstall | bash
Log in:
devtunnel user login
Start hosting a simple service on port 8080 that just echoes any HTTP requests to it:
devtunnel echo http -p 8080
In another command prompt or terminal window, start hosting a dev tunnel for port 8080:
devtunnel host -p 8080
To send 10,000 requests with 100 concurrent connections:
bombardier -c 100 -n 10000 http://yourwebservice.com/api/resource
Confirm that Bombardier is responding by asking for help:
bombardier --help
Create a Web API project:
dotnet new webapi --no-https --no-openapi -o Northwind.WebApi
Create a Web API project for AOT:
dotnet new webapi --no-https --no-openapi -o Northwind.WebApiAot
Publish a project:
dotnet publish
Start the web service using port 5131:
.\Northwind.WebApi --urls="http://localhost:5131"
Start the web service using port 5132:
.\Northwind.WebApiAot --urls="http://localhost:5132"
Use Bombardier to make one million requests using 125 connections (simulated users) to the normal web service, which is listening on port 5131:
bombardier -c 125 -n 1000000 http://localhost:5131/weatherforecast
Use Bombardier to make one million requests using 125 connections (simulated users) to the AOT web service, which is listening on port 5132:
bombardier -c 125 -n 1000000 http://localhost:5132/weatherforecast
Use Bombardier to make as many requests as possible for 10 seconds using 200 connections to the normal web service and output the latency distribution:
bombardier -c 200 -d 10s -l http://localhost:5131/weatherforecast
When running a test project, you can then specify the browser and channel:
dotnet test -- Playwright.BrowserName=chromium Playwright.LaunchOptions.Channel=msedge
Add a new xunit
project named WebUITests
to a Chapter14
solution:
dotnet new sln
dotnet new xunit -o WebUITests
dotnet sln add WebUITests
Install browsers for Playwright to automate:
pwsh playwright.ps1 install
Start the code generator:
pwsh bin/Debug/net8.0/playwright.ps1 codegen https://localhost:5001/
You can start the Playwright Inspector with emulation options like setting a view port size:
pwsh bin/Debug/net8/0/playwright.ps1 codegen --viewport-size=800,600 https://localhost:5001/
You could emulate a device:
pwsh bin/Debug/net8/0/playwright.ps1 codegen --device="iPhone 13" https://localhost:5001/
Run the Playwright PowerShell script with the uninstall
option:
pwsh bin/Debug/net8.0/playwright.ps1 uninstall
To remove browsers of other Playwright installations as well, add the --all
switch:
pwsh bin/Debug/net8.0/playwright.ps1 uninstall --all
For an ASP.NET Core web application that listens on port 8080 within the container, you might want to map it to port 8000 on the host, making the application accessible at http://localhost:8000:
docker run -p 8000:8080 -d --name myaspnetapp mcr.microsoft.com/dotnet/aspnet:latest