Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
vermeir_nico_introducing_net_6_getting_started_with_blazor_m.pdf
Скачиваний:
19
Добавлен:
26.06.2023
Размер:
11.64 Mб
Скачать

Chapter 2 Runtimes and Desktop Packs

While the net6.0-windows TFM is sufficient to get access to the native Windows APIs, it does not contain the specific logic to render WinForms via GDI+ or WPF via DirectX. That logic is contained in the desktop packs. We go over how WinForms and WPF work in more detail in Chapter 4 of this book.

Wrapping Up

While .NET 6 is an easy-to-use and very developer-friendly framework, there is a lot going on under the hood. It has a layered architecture with several runtimes, a

complex three-step compilation process, and even different ways of compiling. All of this complexity is hidden pretty well for us developers; we don’t have to worry that our application will select the Mono runtime on Linux; that is all taken care of for us. However, it is still important to have an idea of what is going on under the hood.

Besides making .NET easy to use, Microsoft had a big challenge with maintaining the cross-platform dream while still being able to provide access to platform-native APIs. Not only for new applications written in new technologies but also for more mature frameworks like WPF and WinForms. Multiple extensions of .NET 6 were created to solve this. We can use these extensions by targeting the correct Target Framework Moniker. Add the desktop packs to this and the cross-platform, native story with full legacy support is complete.

29

CHAPTER 3

Command Line Interface

.NET 6 ships with an extensive set of command line interface (CLI) tools. The toolset allows us to develop, build, run, test, and execute .NET applications on multiple platforms. The .NET 6 CLI ships as part of the .NET 6 SDK.

In this chapter, we will go over the most commonly used commands in the CLI. We will learn about creating projects, restoring dependencies, and even compiling and deploying applications. You will see that what we do from Visual Studio is not magic of the editor but it just triggers these command line tools.

Table 3-1 shows an overview of the basic commands included with the .NET 6

CLI. The complete CLI documentation can be found at ­https://docs.microsoft.com/ en-us/dotnet/core/tools/.

31

© Nico Vermeir 2022

N. Vermeir, Introducing .NET 6, https://doi.org/10.1007/978-1-4842-7319-7_3

Chapter 3 Command Line Interface

Table 3-1.  Basic CLI commands

Command

Explanation

 

 

New

Create a new project using a project template

Restore

Restore dependencies to the project

Build

Compile the project

Publish

Outputs the binaries of an application to a folder for deployment

Run

Execute the application

Test

Run automated tests included in the project

Vstest

Superseded by test

Pack

Packages the project as a NuGet package

Migrate

Migrates old .NET Core 2 projects to newer SDK-style projects

Clean

Cleans project output

Sln

Lists or modifies the project in a solution

Help

Shows more information on a command

Store

Deploys assemblies to the runtime package store

 

 

Besides basic commands, there are also commands that can directly modify a project. Table 3-2 lists the project modification commands.

Table 3-2.  Project modification commands

Command

Explanation

 

 

Add package

Adds NuGet package to the project

Add reference

Adds project reference to the project

Remove package

Removes NuGet package from the project

Remove reference

Removes project reference from the project

List reference

Lists project references

 

 

We also have some more advanced commands at our disposal. Table 3-3 lists the advanced commands with a short description.

32

Chapter 3 Command Line Interface

Table 3-3.  Advanced .NET CLI commands

Command

Explanation

 

 

Nuget delete

Deletes or unlists a package from a NuGet server

Nuget locals

Clears or lists local NuGet resources

Nuget push

Pushes a package to a NuGet server and lists it

Msbuild

Builds a project and all dependencies

 

 

The .NET CLI manages more than just projects. We can use it to install extra tools; an example of this is shown in Chapter 6 with maui-check, which is a tool that verifies that everything is installed correctly to start doing mobile development with .NET. Table 3-4 shows the available commands to manage tools in .NET.

Table 3-4.  Commands for .NET tools

Command

Explanation

 

 

Tool install

Installs a .NET tool

Tool list

Lists all installed tools

Tool update

Updates a tool to the latest version

Tool restore

Installs all tools referenced in a manifest file

Tool run

Runs a tool

Tool uninstall

Removes a tool from the system

 

 

Dotnet New

The dotnet new command can create a new project, configuration file or solution, based on a template.

Listing 3-1 shows the help for the command as a reference.

33

Chapter 3 Command Line Interface

Listing 3-1.  Help page for dotnet new

dotnet new --help Usage: new [options]

Options: -h, --help

-l, --list <PARTIAL_NAME>

-n, --name

-o, --output -i, --install -u, --uninstall --interactive

--add-source, --nuget-source --type

--dry-­run

--force

-lang, --language

--update-check --update-apply --search <PARTIAL_NAME>

Displays help for this command.

Lists templates containing the specified template name. If no name is specified, lists all templates.

The name for the output being created. If no name is specified, the name of the output directory is used.

Location to place the generated output. Installs a source or a template package. Uninstalls a source or a template package. Allows the internal dotnet restore command to stop and wait for user input or action (e.g., to complete authentication).

Specifies a NuGet source to use during install.

Filters templates based on available types. Predefined values are "project" and "item". Displays a summary of what would happen if the given command line were run if it would result in a template creation.

Forces content to be generated even if it would change existing files.

Filters templates based on language and specifies the language of the template to create.

Check the currently installed template packages for updates.

Check the currently installed template packages for update, and install the updates. Searches for the templates on NuGet.org.

34

The target framework for the project. netcoreapp3.0 - Target netcoreapp3.0 netcoreapp3.1 - Target netcoreapp3.1 net5.0 - Target net5.0 net6.0 - Target net6.0
Default: net6.0
Sets langVersion in the created project file text - Optional

 

Chapter 3 Command Line Interface

--author <Author>

Filters the templates based on the template

 

author. Applicable only with --search or

 

--list | -l option.

--package <PACKAGE>

Filters the templates based on NuGet package

 

ID. Applies to --search.

--columns <COLUMNS_LIST>

Comma separated list of columns to display in

 

--list and --search output.

 

The supported columns are language, tags,

 

author, type.

--columns-all

Display all columns in --list and

 

--search output.

--tag <TAG>

Filters the templates based on the tag.

 

Applies to --search and --list.

--no-update-check

Disables checking for the template package

 

updates when instantiating a template.

Help can also be requested per project type. Listing 3-2 shows the output for dotnet new wpf –help, for example.

Listing 3-2.  Help for WPF

WPF Application (C#) Author: Microsoft

Description: A project for creating a .NET WPF Application Options:

-f|--framework

--langVersion

35

Chapter 3 Command Line Interface

--no-restore

If specified, skips the automatic restore of the project

 

on create.

 

bool - Optional

 

Default: false

--nullable

Whether to enable nullable reference types for this project.

 

bool - Optional

 

Default: true

WPF is one of the available templates in .NET 6. For a complete list of all available templates on your machine, you can run dotnet new --list or dotnet new -l.

Figure 3-1.  List of installed templates

36

Chapter 3 Command Line Interface

This list can be extended by installing other templates, either manually or through the command line. Third parties can provide their own templates as well. Let’s say, for example, we want to create an Uno app through the command line (https://platform. uno/). I have the Uno SDK installed on my machine, but not the dotnet new templates. According to their docs, I can create a new Uno project using dotnet new unoapp. Figure 3-1 shows the result.

Figure 3-2.  Trying to create a new project without template

Looking at the documentation for dotnet new, we can see that there’s an --install flag that “Installs a source or a template pack.” From the Uno docs, we can find a command to download and install their template pack. Figure 3-2 shows the templates after installing.

Figure 3-3.  Output after installing the Uno templates

Let’s try the dotnet new command again.

Figure 3-4.  Successfully created a new Uno project through the command line

37