Linux (Windows Linux subsystem)
If you want to experience developing and running. NET core applications in a Linux environment, we have a variety of options. One is to install native Linux on a physical machine, and we can choose some kind of Linux distribution according to our own preference, for now like Rhel, Ubuntu, Debian, Fedora, The mainstream distribution of CentOS and SuSE are supported. If readers think this way is troublesome, we can also install the corresponding Linux distribution in the form of virtual machine, for example, I often use Ubuntu installed on VirtualBox. For 64 of Windows 10 users, we have a third option, which is the Linux subsystem provided by Windows 10 (wsl:windows Subsystem for Linux), Next, let's demonstrate running a. NET core app on WSL.
first, enable the Linux subsystem
Let's take a brief look at how to enable the Linux subsystem. The first thing to remind readers is that only 64-bit Windows 10 Anniversary versions provide support for the Linux subsystem, and if your version of Windows 10 is older, you need to upgrade it to a newer version. After this, we need to turn on the system Developer mode (Developer mode), we only need to select "Settings (Settings)" > "Update and Security" to open the following dialog box. All we need to do is select "Developer (for Developers)" On the left, and in the three options available on the right, select "Dev mode (Developer model)".
After the system's developer mode is enabled, we need to open the Programs and Features dialog box shown in Control Panel > Programs and Features (Programs and Features) and select Turn on or turn off Windows features (Turn Windows features on or off) to open the Windows Features dialog box. Windows subsystem features for Linux are officially enabled when we tick the "Windows Subsytem for linux" option for Linux and reboot.
When it comes to Windows subsystem features for Linux, the system gives us a command called Lxrun to manage this subsystem, which we can use to install, uninstall, and update the Linux system. So far, we just turned on the Linux subsystem this Windows feature, and no specific Linux distribution was installed, so what we need to do now is to execute "lxrun/install" in the form shown command to install a specific Linux distribution, the command currently installed for us is Ubuntu. Once the Linux subsystem is complete, we only need to create a username and password to make a login account.
When Linux is successfully installed, we can execute bash commands on the cmd command line to enter the Linux bash shell. As shown, we will automatically enter the Linux bash shell after executing the bash command on the cmd command line. We execute the lsb_release-a command in the bash shell to output the version information of the currently installed Linux distribution. If you want to revert to cmd command-line mode, we only need to exit the bash shell by executing the exit command.
Windows and Linux subsystems as the primary system can share network systems and file systems. The file system under Windows is mounted directly to the "/mnt" directory, so the file or directory path under Windows with the line "/mnt" prefix becomes the path based on the Linux subsystem. As shown in 1-25, the current working directory in CMD command line environment is "C:\users\jinnan", when we switch to bash shell, the current working directory does not change, but the path becomes "/mnt/c/users/jinnan "only.
II. Installation and development environment
If we want to run. NET core applications under Linux, we just need to install the. NET Core SDK. The installation is slightly different for different Linux distribution,.net Core SDKs. For mainstream Linux distributions (RHEL, Ubuntu, Debian, Fedora, CentOS, SUSE, etc.), a detailed tutorial on installing the. NET Core SDK is given at the official site of. NET core, where we target the WSL version ( Ubuntu 16.04.2 ) briefly describes how to install the corresponding SDK.
For Ubuntu to install the. NET Core SDK, we just need to execute the Apt-get installation of the corresponding Deb package, but before we do, we have to make sure that the URL of the feed where the package is located is locally registered. As for the registration of the feed URL, there are mainly two steps, the first step is to register the signature key (Signature key) file used to verify the integrity of the Deb package, the key encoded content (ASCII Armor) can be directly from the address "https:// Packages.microsoft.com/keys/microsoft.asc "Download. In order to register the key file locally, we execute the following command to download the file and execute "GPG--dearmor" to convert the text content into binary saved in the file "MICROSOFT.GPG", eventually saving the latter to the trusted key directory "/etc/apt/ Trusted.gpg.d "under.
~$ 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 for the signing key file, we can execute the following command to create a dotnetdev.list file in the "/ETC/APT/SOURCES.LIST.D" directory that represents the Deb package source, and the corresponding feed for the. NET Core SDK Deb Packages The URL (Https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod) is included in this file. After that, all of the Deb packages provided by Microsoft through the feed can be installed directly via 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 for the Deb package has been successfully registered and synchronized by executing the apt-get Update command, we can execute the Apt-get install command as follows for installing the. NET Core SDK. It is worth mentioning that if you have installed the previous version locally, you might want to uninstall it first.
~$ sudo apt-get update~$ sudo apt-get install dotnet-sdk-2.0.0
After the installation is complete, we can execute the dotnet–info command to view the basic information of the current. NET Core SDK, and if you get the output as shown, it proves that the. NET Core SDK was successfully installed.
iii. Creating an ASP. NET Core MVC application using the command line
The. NET Core SDK also provides dotnet as an important command-line tool for Linux, which means that we can also create a corresponding. NET core application using the scaffolding template it provides. This time we create an ASP. NET Core MVC, so we specify the template name as "MVC" when we execute the dotnet new command. As shown, the current working directory when we execute the dotnet new command is "/mnt/c/", which is "C: \" Under the main system windows, so the ASP. NET Core MVC application is created with the root directory "C:\helloworld", We have listed all the files and subdirectories in that directory. If we execute the same command under Windows to create an ASP. NET Core MVC command, it also has the same directory structure and file contents.
Like applications created with scaffolding commands under Windows and Mac OS X, we can also run them directly under Linux without any changes to them, so we just need to execute the dotnet Run command in the same directory as the current application. As shown, the start-up ASP. NET Core MVC application is bound on port 5000 for request listening, because Linux is the current Windows subsystem rather than the virtual machine, so the main subsystem can share the network, so we use the browser to request the address "http:// localhost:5000 "can access the home page of this app.
. 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
Linux (Windows Linux subsystem)