Introduction to the .NET CLI
The .NET Command-Line Interface (CLI) is a cross-platform toolchain that enables you to develop, build, test, and deploy .NET applications. It's an essential tool for any .NET developer, especially when working with .NET Core and integrating with cloud platforms like Azure.
What is the .NET CLI?
The .NET CLI is a set of commands that allow you to interact with the .NET SDK. It provides a consistent way to manage your projects and applications across different operating systems (Windows, macOS, Linux). This includes creating new projects, restoring dependencies, building code, running applications, and publishing them for deployment.
The .NET CLI is your primary tool for managing .NET projects from the command line.
It simplifies common development tasks like creating, building, and running applications, making your workflow more efficient.
The .NET CLI is part of the .NET SDK. When you install the .NET SDK, you get access to commands like dotnet new
, dotnet build
, dotnet run
, and dotnet publish
. These commands abstract away complex build processes and provide a standardized interface for developers. This is particularly powerful for automation and scripting, which are crucial for CI/CD pipelines and cloud deployments.
Key .NET CLI Commands
Let's explore some of the most fundamental .NET CLI commands you'll use regularly.
Command | Description | Example Usage |
---|---|---|
dotnet new | Creates a new .NET project or solution from a template. | dotnet new console -o MyConsoleApp |
dotnet restore | Restores the dependencies for a project. | dotnet restore |
dotnet build | Builds a .NET project. | dotnet build |
dotnet run | Runs a .NET application. | dotnet run |
dotnet publish | Publishes a .NET application for deployment. | dotnet publish -c Release |
dotnet test | Runs unit tests in a project. | dotnet test |
Creating a New Project with the CLI
The
dotnet new
dotnet new console -o MyConsoleApp
Building and Running Applications
Once your project is created, you'll use
dotnet build
dotnet run
The .NET CLI orchestrates the build process. When you run dotnet build
, the SDK invokes the compiler (Roslyn for C#) to transform your source code into Intermediate Language (IL) assemblies. These assemblies are then packaged into a deployable format. The dotnet run
command, on the other hand, compiles the project if necessary and then executes the compiled output, often starting with the Program.Main
method.
Text-based content
Library pages focus on text content
Publishing for Deployment
The
dotnet publish
Consider using dotnet publish -c Release
for optimized performance in production environments.
Integration with Azure
The .NET CLI is instrumental in deploying .NET applications to Azure services like Azure App Service, Azure Functions, and Azure Kubernetes Service (AKS). You can use CLI commands to package your application and then deploy it using Azure CLI (
az
dotnet publish
Learning Resources
Official Microsoft documentation providing a comprehensive overview of the .NET CLI, its commands, and usage.
A step-by-step tutorial guiding you through the basic commands of the .NET CLI for creating and managing projects.
A video tutorial explaining the core concepts and benefits of using the .NET CLI for .NET development.
A blog post offering practical tips and advanced usage scenarios for the .NET CLI.
Detailed reference for the `dotnet new` command, including available templates and options.
In-depth documentation on the `dotnet publish` command, covering deployment strategies and options.
Introduction to the Azure CLI, the command-line tool for managing Azure resources, which complements the .NET CLI for cloud deployments.
A tutorial showing how to deploy a .NET application to Azure App Service, often using the .NET CLI.
A handy cheat sheet summarizing common .NET CLI commands for quick reference.
The official download page for the .NET SDK, which includes the .NET CLI.