. NET Core multi-platform development experience [3]: Linux (Windows Linux subsystem), corelinux
If you want to develop and run a. NET Core application in a Linux environment, we have multiple options. One is to install native Linux on a physical machine. We can choose a Linux Distribution based on our preferences, currently, mainstream Distribution such as RHEL, Ubuntu, Debian, Fedora, CentOS, and SUSE are supported. If readers think this method is troublesome, we can also install the corresponding Linux Distribution in the form of virtual machines. For example, I often use Ubuntu installed on VirtualBox. For users with 64 Windows 10, we have a third option, that is, the Linux Subsystem (WSL: Windows Subsystem for Linux) provided by Windows 10 ), next we will demonstrate how to run on WSL.. NET Core application.
1. Enable the Linux Subsystem
Next, we will briefly introduce how to enable the Linux subsystem. The first choice is to remind readers that only the 64-bit version of Windows will support the Linux subsystem. If your version of Windows 10 is old, you need to upgrade it to a newer version. After that, we need to enable the Developer Mode of the system. To do this, we only need to select "Settings"> "Update and Security) "to open the following dialog box. Select "For developers" on the left and select "Developer Model" from the three options provided on the right )".
After the system's developer mode is enabled, we need to go through "Control Panel"> "Programs and Features) "Open the" programs and functions "dialog box as shown in, and select" Turn Windows features on or off "to open the" Windows functions "dialog box. After selecting the "Windows subsystem for Linux (Windows Subsytem for Linux)" option and restarting it, the Windows subsystem features for Linux are officially enabled.
After the features of the Windows subsystem for Linux, the system gives us a command named lxrun to manage this subsystem. We can use it to install, uninstall, and update the Linux system. So far, we have only enabled the Windows feature of the Linux subsystem, and no specific Linux Distribution has been installed, so what we need to do now is to execute the "lxrun/install" command in the form shown in to install a specific Linux Distribution. This command is currently installed on Ubuntu. After the Linux sub-system is complete, we only need to create a user name and password to create a login account.
After Linux is successfully installed, run the bash command on the CMD command line to enter the Linux Bash Shell. As shown in, after executing the bash command in the CMD command line, the system automatically enters the Linux Bash Shell. Run the lsb_release-a command in Bash Shell to output the version of the currently installed Linux Distribution. To restore to the CMD command line mode, run the exit command to exit the Bash Shell.
As the main system, Windows and Linux subsystems can share network systems and file systems. The file system in Windows is directly mounted to the "/mnt" directory. Therefore, the file or directory path in Windows is prefixed with "/mnt", which is based on the path in the Linux subsystem. As shown in figure 1-25, the current working directory in the CMD command line environment is "c: \ users \ jinnan". When we switch to the Bash Shell, the current working directory does not change, the path is changed to "/mnt/c/users/jinnan.
Ii. Install the development environment
If you want to run the. NET Core application in Linux, you only need to install the. NET Core SDK. For different Linux Distribution,. NET Core SDK installation is slightly different. For mainstream Linux releases (such as RHEL, Ubuntu, Debian, Fedora, CentOS, and SUSE ),. NET Core official site provides the installation. NET Core SDK. Here we will briefly introduce how to install the corresponding sdk for the wsl version (Ubuntu 16.04.2.
For Ubuntu installation. NET Core SDK, we only need to execute apt-get to install the corresponding deb package, but before that, we need to ensure that the URL of the Feed where the package is located is registered locally. The registration of the Feed URL involves two steps. The first step is to register the Signature Key file used to verify the integrity of the deb package, the content (ASCII Armor) after the key is encoded can be downloaded directly from the address "https://packages.microsoft.com/keys/microsoft.asc.pdf. To register the key file locally, run the following command to download the file and run "gpg -- dearmor" to convert the text content to binary and save it in the file "microsoft. gpg, and finally save the latter to the trusted Key Directory "/etc/apt/trusted. gpg. d.
~$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
~$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
After registering the signature key file, run the following command to indicate "/etc/apt/sources" in the deb package source. list. d "directory to create a dotnetdev. list file ,. the Feed URL (https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod) corresponding to the deb package where the NET Core SDK is located is contained in this file. After that, Microsoft can install all the deb packages provided by the Feed directly using the apt-get command.
~$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
After the Feed of the deb package is successfully registered and the apt-get update command is executed for synchronization, we can execute the apt-get install command to install it as follows.. NET Core SDK. It is worth mentioning that if you have installed earlier versions locally, you 'd better uninstall them first.
~$ sudo apt-get update
~$ sudo apt-get install dotnet-sdk-2.0.0
After the installation is complete, run the dotnet-info command to view the basic information of the current. NET Core SDK. If the output is as shown in, the. NET Core SDK is successfully installed.
3. Create an ASP. NET Core MVC application using command lines
The. NET Core SDK also provides the dotnet command line tool for Linux, which means that we can also use the scaffold template provided by it to create the corresponding. NET Core application. This time, we directly create an ASP. NET Core MVC. Therefore, when executing the dotnet new command, we must specify the Template Name as "mvc ". As shown in, the current working directory when we execute the dotnet new command is "/mnt/c/", that is, "c: \" in the main system Windows :\", so the created ASP. the root directory of the NET Core MVC application is "c: \ helloworld". We list all the files and subdirectories in this directory. If we execute the same command in Windows to create an ASP. NET Core MVC command, it also has the same directory structure and file content.
Like Applications created using scaffolding commands in Windows and Mac OS X, we can run them directly in Linux without making any changes to them, therefore, we only need to execute the dotnet run command in the directory where the current application is located. As shown in, ASP after startup. by default, the NET Core MVC application is bound to port 5000 for request listening. Because Linux is the current Windows subsystem rather than a virtual machine, the main and sub-systems can share the network, therefore, we can use the browser request address "http: // localhost: 5000" to access the homepage of this application.
. NET Core multi-platform development experience
. NET Core multi-platform development experience [1]: Windows
. NET Core multi-platform development experience [2]: Mac OS X
. NET Core multi-platform development experience [3]: Linux
. NET Core multi-platform development experience [4]: Docker