Introduction

In this post we are going to learn about some of the tools used for creating .NET Core applications. .NET Core applications can be created with or without the Visual Studio. In this post we are going to look at the cross platform command line tools, such as DNX, DNVM and DNU, to create .NET Core applications.

DNX

Before we start learning about DNX it is helpful if we take a brief look at how .NET applications are executed.

How .NET Applications are executed ?

Before any managed code can be executed, the Common Language Runtime (CLR) must be loaded into the current process and initialized so that it can run the managed code. This process is called as runtime hosting and it is performed by runtime hosts. All the .NET Framework applications require a piece of code called as Runtime Host to host the CLR. All the application models, like windows forms, WPF, ASP.NET etc., provide runtime hosts to host the CLR. For example, when you compile your managed code as an .exe assembly, the runtime is started automatically by mscoree.dll when the .exe is run. All hosts start with an unmanaged stub (for example a C++ or C code) because the runtime is not yet running in the process. The runtime host loads the runtime into a process, creates the application domains within the process, and loads user code into the application domains.

What Is DNX ?

The .NET Execution Environment (DNX) is a software development kit (SDK) and runtime environment that has everything you need to build and run .NET applications for Windows, Mac and Linux. It provides a host process, CLR hosting logic and managed entry point discovery. DNX was built for running cross-platform ASP.NET Web applications, but it can run other types of .NET applications, too, such as cross-platform console apps.

DNX is a package that contains -

While reading following description of DNX use the below mentioned DNX command as a reference.

dnx . web

which is equivalent to

dnx.exe --lib {paths} --appbase . Microsoft.Framework.ApplicationHost Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000

The URL and the port will be whatever is defined by you. - -lib {paths} are the known paths from where the compiled assemblies will be loaded (CoreCLR and other DNX assemblies will be loaded from here).

  1. Native Process (such as DNX.exe in the above command) whose responsibility is to find and call the CLR Native host, passing arguments given to the process to the native host to be used by the rest of the stack. The selected native CLR host will be specific to the version of the CLR that is being used.
  2. Native CLR Host that has following main responsibilities -
    1. Boot the CLR, how this is achieved depends on the version of the CLR.
    2. Call the Managed Entry Point.
    3. When the managed entry point returns cleanup and shutdown the CLR.
  3. Managed Entry Point which is responsible for -
    1. Creating a LoaderContainer which will resolve the required assemblies using various Loaders.
    2. Creating a root loader that will resolve all the DNX assemblies.
    3. Call the main entry point of the provided program. DNX package also provides an EntryPointExecutor which contains the logic to find a Main method on the provided assembly, with support for both static and non-static classes. The “provided program” could be an application compiled to assemblies on disk and invoked directly. However, in our case (in the command above) and most of the cases this program is the generic application host provided by the runtime (Microsoft.Framework.ApplicationHost).
  4. Application Host which is responsible for -
    1. Providing support for the new project structure (project.json) i.e. walk the dependencies in the project.json and build up the closure of dependencies the application will use.
    2. Add new loaders to the LoaderContainer (initialized previously by the Managed Entry point) that can load assemblies from various sources, NuGet, Roslyn, etc.
    3. Call the entry point of the assembly (in our case Microsoft.AspNet.Hosting from the above command), whose name is given as the next argument to the native process, with the help of EntryPointExecutor.
  5. DNU, a tool, which is responsible for all operations involved with packages in your application such as installing, updating, publishing packages etc.

How to get DNX ?

You can use DNVM to get various DNXs. DNVM is explained in the next section of this post.

DNVM

.NET Version Manager

What is DNVM ?

It is a cross platform (runs on Windows, OS X and Linux) command line utility to manage various DNXs on any machine. DNVM has various commands, such as install, list, use, upgrade etc., to manage DNXs on the machine. DNVM is simply a script file (PowerShell script file on Windows) that uses predefined feeds (NuGet for stable versions and MyGet for unstable versions) to download the DNXs, place them in a well-known location and add environment variables for the user to be able to use them.

How to get DNVM ?

If you have installed Visual Studio 2015 then you already have the DNVM installed. To find out the path where DNVM is installed run following command from the command prompt.

where dnvm

DNX/DNVM isn't actually installed until you run File -> New after VS 2015 is installed, so if you have VS 2015 installed but have not opened a new file yet, you won't have it on your machine yet.

If you want to install DNVM without the Visual Studio then you can find instructions here and here.

DNVM is installed at %userprofile%/.dnx/bin.

In the beginning there might not be any DNXs installed so the DNVM will give you an option to install one.

DNU

DNX Utility

What is DNU ?

DNU is a command line utility used to build, package and publish DNX applications. It is mainly responsible for all the operations involved with packages in our application. For example Restore, Install, Pack, Publish, List etc. DNU uses NuGet behind the scene to provide all the package management and deployment functionality.

How to get DNU ?

DNU comes packaged with DNX.

Summary

In this post we looked at various cross platform command line utilities, like DNX, DNVM and DNU, that allow us to develop .NET Core applications. We will look at these utilities in more details in future posts.

References

  1. Official ASP.NET Documentation This article from official ASP.NET documentation site provides somewhat broad overview of DNX.
  2. CLR Hosting OverviewLoading CLR into a Process and Runtime Hosts These MSDN articles provides information about how .NET Framework applications are executed.
  3. Structure of DNX Provides detail information about the structure of DNX.
  4. An inside look at ASP.NET 5 execution Part 1, Part 2, Part 3, Part 4, Part 5 and Part 6 This series provides an in depth look at DNX application execution process.
  5. Overview of .NET Ecosystem in 2015 This article provides overview of .NET Ecosystem in 2015 and brief information about various tools used for developing .NET Core applications.
  6. DNVM, DNX and DNU - Understanding the ASP.NET 5 Runtime Options In depth post about usage of DNX, DNVM and DNU.