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

Chapter 3 Command Line Interface

--self-contained Self-contained applications include the .NET runtime your application specifies so that it doesn’t need to be installed on the target machines. This eliminates the risk of having to install multiple versions of the .NET runtime on a

machine, potentially causing conflicts. Shipping the .NET runtime with your application greatly simplifies installation, but it does come with a warning. Software contains bugs, especially in complex software like .NET; these bugs can show up pretty late in their lifespan and potentially cause severe security issues. Companies like Microsoft are usually quite fast to fix those security issues and push out an update. If you are packaging a specific .NET runtime version with your app, it is up to you as application developer

to follow up on those .NET updates and update your application with the new version of the .NET runtime when needed. In other words, you are responsible for replacing vulnerable .NET binaries in your application.

--no-self-contained does not ship the .NET runtime with the application. In this case, the application relies on the version of the .NET runtime installed on the user’s machine.

Dotnet Run

Dotnet run runs your application. It builds the application using dotnet build and launches the application.

Listing 3-16.  Documentation on dotnet run

Usage:

dotnet [options] run [[--] <additional arguments>...]]

Options:

 

-c, --configuration <CONFIGURATION>

The configuration to run for.

 

The default for most projects is

 

'Debug'.

-f, --framework <FRAMEWORK>

The target framework to run for.

 

The target framework must also be

 

specified in the project file.

-r, --runtime <RUNTIME_IDENTIFIER>

The target runtime to run for.

56

 

Chapter 3 Command Line Interface

--project <project>

The path to the project file to run

 

(defaults to the current directory

 

if there is only one project).

-p, --property <property>

Properties to be passed to MSBuild.

--launch-profile <launch-profile>

The name of the launch profile (if

 

any) to use when launching the

 

application.

--no-launch-profile

Do not attempt to use

 

launchSettings.json to configure the

 

application.

--no-build

Do not build the project before

 

running. Implies --no-restore.

--interactive

Allows the command to stop and wait

 

for user input or action (e.g., to

 

complete authentication).

--no-restore

Do not restore the project before

 

building.

--sc, --self-contained

Publish the .NET runtime with your

 

application so the runtime doesn't

 

need to be installed on the target

 

machine.

 

The default is 'true' if a runtime

 

identifier is specified.

--no-self-contained

Publish your application as a

 

framework-dependent application.

 

A compatible .NET runtime must be

 

installed on the target machine to

 

run your application.

-v, --verbosity <LEVEL>

Set the MSBuild verbosity level.

 

Allowed values are q[uiet],

 

m[inimal], n[ormal], d[etailed], and

 

diag[nostic].

-a, --arch <arch>

The target architecture.

--os <os>

The target operating system.

-?, -h, --help

Show command line help.

57

Chapter 3 Command Line Interface

Dotnet run executes the binaries in the bin/<config>/<TFM> folder. This is where the output of the build command is put. In case of .NET 6, this could be /bin/<Configuration>/net6.0. Other frameworks can be specified by using the --framework parameter. A configuration can be specified by using the --configuration parameter. Listing 3-17 shows a complete example of a dotnet run command.

Listing 3-17.  Running a .NET 6 application using CLI

dotnet run --configuration Release --framework net6.0 --project

.\CliDemo.csproj

Dotnet Test

Dotnet test is used to run unit tests included in a project.

Listing 3-18.  dotnet test documentation (shortened for brevity)

Usage:

dotnet [options] test [<PROJECT | SOLUTION>...]

Arguments:

<PROJECT | SOLUTION> The project or solution file to operate on. If a file is not specified, the command will search the current directory for one.

Options:

 

-s, --settings <SETTINGS_FILE>

The settings file to use when

 

running tests.

-t, --list-tests

List the discovered tests instead

 

of running the tests.

-e, --environment <NAME="VALUE">

Sets the value of an environment

 

variable.

 

Creates the variable if it does not

 

exist and overrides if it does

 

This will force the tests to be run

 

in an isolated process.

 

This argument can be specified

 

multiple times to provide multiple

 

variables.

58

Chapter 3 Command Line Interface

Dotnet test commands trigger a dotnet build after which it executes any unit tests it finds. It supports any test framework that has support for .NET 6, for example, MSTest, NUnit, XUnit, etc. Every test that was run by the tool will print its result; after all tests are run, the dotnet test command will return to either 0 (all tests successful) or 1 (at least one test failed). Figure 3-8 shows an example output of dotnet test.

Figure 3-8.  Example output of dotnet test

In the screenshot, you can see that four tests passed, while one failed. The tests in this sample project are written using NUnit; dotnet test automatically picks up on this and uses the correct adapter. Should it not find the correct adapter, or you want to use a custom one, you can use the --test-adapter-path option of the dotnet test command. The custom adapter needs to be named *.TestAdapter.dll for it to be picked up by the

.NET CLI tooling. Figure 3-6 also shows that the first step in running the test command is verifying if dependencies need to be restored or if the project needs to be compiled.

After fixing the failing unit test, we get the result shown in Figure 3-9.

Figure 3-9.  Passing all unit tests

Other often used options in dotnet test are:

•\

--configuration same as with dotnet build and dotnet deploy, sets the

 

configuration to be used.

•\

--collect enables data collection, for example, code coverage.

59