![](/user_photo/_userpic.png)
- •Table of Contents
- •About the Author
- •Acknowledgments
- •Introduction
- •Version Support
- •Supported Versions
- •A Unified Platform
- •Roadmap
- •Supported Operating Systems
- •Command Line Interface
- •Desktop Development
- •Blazor
- •MAUI
- •Wrapping Up
- •.NET 6 Architecture
- •Runtimes
- •CoreCLR
- •Mono
- •WinRT
- •Managed Execution Process
- •Desktop Packs
- •Wrapping Up
- •Dotnet New
- •Dotnet Restore
- •NuGet.config
- •Dotnet Build
- •Dotnet Publish
- •Dotnet Run
- •Dotnet Test
- •Using the CLI in GitHub Actions
- •Other Commands
- •Wrapping Up
- •WinAPI
- •WinForms
- •STAThread
- •WinForms Startup
- •DPI Mode
- •Responding to Scale Events
- •Visual Styles
- •Text Rendering
- •The Message Loop
- •The Form Designer
- •WPF Startup
- •XAML Layout
- •Visual Tree
- •Data Binding
- •Windows App SDK
- •Building a Windows App SDK application
- •Using Windows APIs with Windows App SDK
- •Packaging
- •Migrating to .NET 6
- •Upgrade Assistant
- •Wrapping Up
- •Blazor WebAssembly
- •Creating a Blazor Wasm Project
- •Blazor Progressive Web Apps
- •Exploring the Blazor Client Project
- •Blazor in .NET 6
- •Blazor Component System
- •Creating Blazor Pages
- •Running a Blazor App
- •Blazor Server
- •SignalR
- •Blazor Desktop
- •Wrapping Up
- •Project Structure
- •Exploring MAUI
- •The Cross-Platform World
- •Application Lifecycle
- •MVVM
- •MVVM Toolkit
- •Wrapping Up
- •Model-View-Controller
- •Routing
- •Views
- •Controllers
- •Controller-Based APIs
- •Minimal APIs
- •Wrapping Up
- •Web Apps
- •Creating an App Service
- •Static Web Apps
- •Web App for Containers
- •Docker
- •Azure Functions
- •Deploying Azure Functions
- •Wrapping Up
- •Record Types
- •Monolith Architecture
- •Microservices
- •Container Orchestration
- •Kubernetes
- •Docker Compose
- •Dapr
- •Installing Dapr
- •Dapr State Management
- •Wrapping Up
- •Roslyn
- •Compiler API
- •Diagnostic API
- •Scripting API
- •Workspace API
- •Syntax Tree
- •Roslyn SDK
- •Source Generators
- •Writing a Source Generator
- •Debugging Source Generators
- •Wrapping Up
- •Garbage Collector
- •The Heap
- •The Stack
- •Garbage Collection
- •A Look at the Threadpool
- •Async in .NET 6
- •Await/Async
- •Cancellations
- •WaitAsync
- •Conclusion
- •Index
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
![](/html/75672/2303/html_CACnH9VZ6O.sqXh/htmlconvd-Yz4fCu69x1.jpg)
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