DotNet CLI

  • Difference between DNVM Install and Upgrade Commands

    In the last post I have explained the use of install and upgrade commands for managing the runtimes in DNVM. If you have missed that post or wants to recap it, refer it here

    The install and upgrade commands are mainly used for installing the runtimes, but there are some difference between those and let's examine what are those in this post.

    Let's first list all the environments in the machine using the command dnvm list and will try to install a new runtime.

    Install Command

    From the list we can see that we are missing the Release Candidate Update 2 based on 64-bit version for the clr framework. So let's install it using the following command.

    dnvm install -arch x64 -r   clr 1.0.0-rc1-update2

    When you run the command, it will download the packgage from the feed and installs it to the runtimes folder in your user profiles. Then it will add the path to the process path variable and also makes it as the active one which is indicated by the * sign in the below screenshot. 

    But the one disadavantage is that the selected runtime is active only for the duration of the current session in the command prompt. Meaning it will revert to none or an earlier one if I close the window and open it again.

    If you compare the screenshots for the output of dnvm list commands, you can see that before installing it, the active version was 1.0.0-rc1-update 2 targeting coreclr based on x86. But after installing it's now 1.0.0.-rc1-update2 targeting clr based on x64. But if I close and reopen the terminal window, the active one will be the former one

    Upgrade Command

    The upgrade command also installs the runtime, but the difference is that it downloads and installs the runtime version which is the latest one.

    dnvm upgrade -arch x64 -r  clr 

    It will download and install the latest 64-bit version of clr framework.

    The difference is that it will add to the user path as well as process variables as active version, so that the selection will be persisted and won't revert back when we close and open the command window. It will also add an alias for the runtime.

     


  • Manage Runtimes Using DNVM Install and Upgrade Commands

    DNVM Install and Upgrade commands can be used to install a new version of the runtime or to upgrade an existing version to a newer version of the runtime.

    Install command can be used to download a particular version of the runtime.

    Syntax

    dnvm install –arch <architecture name=""> –r<runtime name="">

    Example

    dnvm install –arch x64 –r coreclr 1.0.0-rc1-update2

    The above command will install the version 1.0.0-rc1-update2 targeting the Core CLR runtime for 64-bit. It installs into runtimes folder located in the user profiles folder and modifies the path variable too.


  • DNX - Using Commands option

    In DNX environment we are using dnx run command to run the program from the command line. I have already authored a post on this and you refer that from here

    In this post I am going to cover the custom command option avaliable in .NET core using which we can do named execution of our program. We can define commands in the project.json file in your project folder and is recognized by editors like Visual Studio and Visual Studio Code. The commands defined in the project folder is available in that project only. If you want you can have commands defined at a global level, then it is available for everthing that runs under a user profile. 

    Sample program 

    using System;
    using static System.Console;
    using System.Collections.Generic;
    namespace DnxCoreSample.Commands
    {
        public class Program
        {
            public void Main(string[] args)
            {
                var items = new List<string>() { "Red", "Blue", "White", "Green" };
                foreach (var item in items)
                {
                    WriteLine($"Color : {item}");
                }
            }
    
            
        }
    }
    

    Let's see how can we configure that in project.json file

    {
      "frameworks": {
        "dnx451": { },
        "dnxcore50": {
          "dependencies": {
            "System.Console": "4.0.0-beta-23516",
            "System.Collections": "4.0.11-beta-23516"
          }
        }
      },
      "commands": {
        "RunSample" : "dnxcommands"
    
      }
    }
    

  • The dnx run Command

    As we have seen in the earlier post, we can use the dnx run command to compile and immediately execute the program. To recap, when you execute the below command in the command prompt, it will execute the program which has an entry point defined in the current folder. 

    dnx run

    I have created a sample program  which outputs a message to the console.

    using System;
    using static System.Console;
    namespace DnxCoreSample.Commands
    {
     public class Program
        {
            public  void Main(string[] args)
            {
                WriteLine("Getting started with dnx commands");
    			
            }
        }
    }	
    

    So if we try to execute the dnx run command, will get an error as shown below.

    That's because we haven't created the configuration file yet, which is used by the DNX environment for managing the dependencies and other project settings. So let's create one with the needed dependencies and run the dnu restore command to download the packages. 

    So after downloading, run the program using dnx run in the command prompt and will get the result as shown below.


  • Hello From DNX

    In the earlier post, I have gone through in detail about setting up dnx environment in your machine without installing Visual Studio. Also I have gone though some of the commands which will help you to the bare necessities in DNVM(.NET Version Manager). If you haven't seen that post yet, please read it from here.

    So we have set up the dnx environment in our system, now let's see how can we compile and run programs using this.

    Let's create a sample C# program using any text editors like Notepad or Sublime or Visual Studio Code. 

    using System;
    namespace DnxCoreSample
    {
     public class Program
        {
            public static void Main(string[] args)
            {
    		Console.WriteLine("Hello World from DNX!");
            }
        }
    }	

    Our program when executed will print a message on to the console. So before compiling an running the code we need to do one more thing. That's setting up the configurations needed for DNX will use to compile and run this program. In .NET Core we have the project.json file where all the project related settings. So create a file named project.json file in your folder and add the following entries

    {
      "frameworks": {
        "dnx451": { },
        "dnxcore50": {
          "dependencies": {
            "System.Console": "4.0.0-beta-23516"
    
          }
        }
      },
    
      "commands": {
        "HelloDnx": "dnxcoresamples"
      }
    }