User Tools

Site Tools


notes:tools:netcore

.NET Core

dotnet Command

Create and run a console app called 'ConsoleApp':

md ConsoleApp
cd ConsoleApp
dotnet new console
dotnet run

Create a console app in a given language and the name 'MyApp':

dotnet new console -lang "F#" --name "MyApp"

Specify command-line arguments. Here, we specify four arguments:

dotnet run arg1 arg-2 arg:3 "arg 4"

Create a library called 'ClassLib':

md ClassLib
cd ClassLib
dotnet new classlib

Create an NUnit3 test project:

md TestProj
cd TestProj
dotnet new nunit
 
dotnet test                      # run a test

Add a project reference:

cd TestProj
dotnet add reference "..\ConsoleApp\ConsoleApp.csproj"

Show help:

dotnet --help                    # get help on dotnet commands and options
dotnet <command> -h              # get CLI help (output help at the command line)
dotnet <command> --help          # the same as dotnet <command> -h
dotnet help <command>            # get on-line help

Show help on how to add references:

dotnet add reference -h
dotnet add package -h

Run tests:

cd TestProj
dotnet test
dotnet build
dotnet build -v n            # set verbosity level to normal
dotnet build -f net45        # use a specific target framework; the framework has to be set as <TargetFrameworks>
dotnet build /p:Platform=x64

dotnet commands:

dotnet --version                 # determine which version of the .NET Core SDK you have available
dotnet run                       # build and execute .csproj in the current folder
dotnet <command> -v diag         # run diagnostics
dotnet add app.csproj reference lib.csproj  # add p2p reference to app.csproj

Deployment

# FDD - framework-dependent deployment
dotnet publish -c Release        
 
# SCD - self-contained deployment; publish a self-contained package for a specific platform
# RID - runtime identifier: [os].[version]-[arch]
# RID has to be also specified in csproj's RuntimeIdentifier or RuntimeIdentifiers element.
dotnet publish -c Release -r ubuntu.16.04-x64

csproj MSBuild

Multi-targeting:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.0;net462</TargetFrameworks>
  </PropertyGroup>
</Project>

Enable a specific language version compiler:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>8.1</LangVersion>
  </PropertyGroup>
</Project>

Values for the <LangVersion>:

LangVersion Description
7, 7.1, 7.2, 7.3, 8 Entering a specific version number will use that compiler if it has been installed.
latestmajor Uses the highest major number
latest Uses the highest major and highest minor number
preview Uses the highest available preview version

Your projects must target either netcoreapp3.0 or netstandard2.1 to use the full features of C# 8.0.

Multi-platform Support

Handle platforms that do not support an API using PlatformNotSupportedException.

Install prerequisites for SCD on Ubuntu:

apt update
apt install -y libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev libcurl4-openssl-dev uuid-dev unzip

'which dotnet' displays the path to dotnet on Linux or Mac:

  • Linux: /usr/bin/dotnet
  • Mac: /usr/local/share/dotnet

Folders with dotnet SDK:

  • Windows: C:\Program Files\dotnet\sdk
  • Linux: /usr/bin/dotnet/sdk (error???: “dotnet - not a directory”; maybe because it's in a container)
  • Mac: /usr/local/share/dotnet/sdk

Location of .NET Core installations

  • Windows: C:\Program Files\dotnet\shared
  • 2.0.7, 2.0.9, 2.1.2

x86_x64 Cross Tools CommandPrompt for VS 2019:

csc -langversion:?           # determine which versions of the C# compiler you have available
notes/tools/netcore.txt · Last modified: 2020/09/18 by leszek