dotnet run command - .NET CLI (original) (raw)

This article applies to: ✔️ .NET 6 SDK and later versions

Name

dotnet run - Runs source code without any explicit compile or launch commands.

Synopsis

dotnet run [<applicationArguments>]
  [-a|--arch <ARCHITECTURE>] [--artifacts-path <ARTIFACTS_DIR>]
  [-c|--configuration <CONFIGURATION>] [--disable-build-servers]
  [-e|--environment <KEY=VALUE>] [--file <FILE_PATH>]
  [-f|--framework <FRAMEWORK>] [--force] [--interactive]
  [-lp|--launch-profile <NAME>] [--no-build] [--no-cache]
  [--no-dependencies] [--no-launch-profile] [--no-restore] [--os <OS>]
  [-p|--property:<PROPERTYNAME>=<VALUE>]
  [--project <PATH>] [-r|--runtime <RUNTIME_IDENTIFIER>]
  [--sc|--self-contained] [--tl:[auto|on|off]] [-v|--verbosity <LEVEL>]
  [[--] [application arguments]]

dotnet run -h|--help

Description

The dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code. Any requirements for the build apply to dotnet run as well.

Output files are written into the default location, which is bin/<configuration>/<target>. For example if you have a netcoreapp2.1 application and you run dotnet run, the output is placed in bin/Debug/netcoreapp2.1. Files are overwritten as needed. Temporary files are placed in the obj directory.

If the project specifies multiple frameworks, executing dotnet run results in an error unless the -f|--framework <FRAMEWORK> option is used to specify the framework.

The dotnet run command is used in the context of projects, not built assemblies. If you're trying to run a framework-dependent application DLL instead, you must use without a command. For example, to run myapp.dll, use:

dotnet myapp.dll

For more information on the dotnet driver, see .NET CLI overview.

To run the application, the dotnet run command resolves the dependencies of the application that are outside of the shared runtime from the NuGet cache. Because it uses cached dependencies, it's not recommended to use dotnet run to run applications in production. Instead, create a deployment using the dotnet publish command and deploy the published output.

Implicit restore

You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack. To disable implicit restore, use the --no-restore option.

The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

For information about how to manage NuGet feeds, see the dotnet restore documentation.

This command supports the dotnet restore options when passed in the long form (for example, --source). Short form options, such as -s, are not supported.

Workload manifest downloads

When you run this command, it initiates an asynchronous background download of advertising manifests for workloads. If the download is still running when this command finishes, the download is stopped. For more information, see Advertising manifests.

Arguments

<applicationArguments>

Arguments passed to the application that is being run.

Any arguments that aren't recognized by dotnet run are passed to the application. To separate arguments for dotnet run from arguments for the application, use the -- option.

Forward arguments to the application

dotnet run forwards any token it doesn't recognize to the application. The forwarded tokens keep their original order, but dotnet run first removes the options it understands. When a recognized option appears between an unrecognized option name and its value, removing the recognized option can change the meaning of the leftover tokens.

For example, the following command interleaves the recognized option --project between tokens the application is meant to receive:

dotnet run --app-flag --app-name --project ConsoleApp.csproj A.txt

After dotnet run consumes --project ConsoleApp.csproj, the application receives --app-flag --app-name A.txt. The application then treats A.txt as the value of --app-name, which doesn't match the original command line.

To avoid this ambiguity, place application arguments after a literal --:

dotnet run --project ConsoleApp.csproj -- --app-flag --app-name A.txt

The -- separator marks every following token as an application argument, so dotnet run doesn't reorder or reinterpret them. The separator also future-proofs scripts against new dotnet run options that might later match a token previously forwarded to the application.

Note

The same behavior applies to dotnet build and to dotnet test in Microsoft.Testing.Platform (MTP) mode, which forward unrecognized tokens to MSBuild or to the test application respectively. For more information about dotnet test, see Forward arguments to the test application.

Options

--property:<NAME1>=<VALUE1>;<NAME2>=<VALUE2>  
--property:<NAME1>=<VALUE1> --property:<NAME2>=<VALUE2>  

The short form -p can be used for --property. If the argument provided for the option contains =, -p is accepted as short for --property. Otherwise, the command assumes that -p is short for --project.
To pass --property to the application rather than set an MSBuild property, provide the option after the -- syntax separator, for example:

dotnet run -- --property name=value  

Environment variables

There are four mechanisms by which environment variables can be applied to the launched application:

  1. Ambient environment variables from the operating system when the command is run.
  2. System.CommandLine env directives, like [env:key=value]. These apply to the entire dotnet run process, not just the project being run by dotnet run.
  3. environmentVariables from the chosen launch profile (-lp) in the project's launchSettings.json file, if any. These apply to the project being run by dotnet run.
  4. -e|--environment CLI option values (added in .NET SDK version 9.0.200). These apply to the project being run by dotnet run.

The environment is constructed in the same order as this list, so the -e|--environment option has the highest precedence.

Examples

dotnet run  
dotnet run --file ConsoleApp.cs  

File-based app support was added in .NET SDK 10.0.100.

dotnet run --project ./projects/proj1/proj1.csproj  
dotnet run --property:Configuration=Release  
dotnet run --configuration Release -- --help  
dotnet run --verbosity m  
dotnet run -f net6.0 -- arg1 arg2  

In the following example, three arguments are passed to the application. One argument is passed using -, and two arguments are passed after --:

dotnet run -f net6.0 -arg1 -- arg2 arg3