Saturday, January 22, 2022

WSL 2.0: wget and ca-certificates -- Key apps for Developing with WSL Distribution

The prerequisites needed to prepare a WSL distribution to be used for development include:

  •  ca-certificates: used by applications to validate the authenticity of SSL connections
  • wget: used by applications to download files via standard protocols (HTTP, HTTPS, FTP)

To install ca-certificates and wget on a distribution use the command, sudo apt install wget ca-certificates, as follows:


The apt common with the install parameter was prefixed by sudo which means a user will be prompted for their password in order to be elevated to the level of privileges required to perform the install:

The install of wget and ca-certificates when completed is as follows:


Appendix A: wget

Ubuntu.com's documentation for wget provides the following man page for this application:

Appendix B: ca-certificates



WSL 2.0: Accessing a Windows System for Linux (WSL) Distribution from Windows

 The file system for each Windows System for Linux (WSL) distribution is accessible from Windows using the \\wsl$ folder. Below the \\wsl$ folder is shown for a WSL instance containing a single distribution, Ubuntu-20.04:



WSL 2.0: Accessing Windows Files from a Windows System for Linux (WSL) Distribution

When WSL is installed and a distribution is installed on WSL, the distribution is associated with a file system separated from Windows NTFS file system of the host. Linux's view of file systems is as mount points which can be viewed using: ls -al /mnt | more 

Piping the output of ls to more strips the colors from the output which makes the output readable to certain categories of color-blind engineers (see Ubuntu (for the color blind developer): Removing colors from ls).

The 'c' drive under /mnt is the Windows file system corresponding to the C: drive. The contents of Windows C: can be viewed using ls -al /mnt/c | more as follows:




WSL 2.0: Linux (Ubuntu/Debian) apt Updating and Upgrading Applications

Once a Linux distribution such as Ubuntu or Debian has been installed on WSL 2.0, the applications currently installed can be updated and upgraded using the apt command. This command is invoked as follows to first update and then upgrade existing applications:

sudo apt update && sudo apt upgrade

The command is invoked on WSL as follows:


The sudo command before the apt command allows apt to run with privileges sufficient to run administrative tasks (such as application update and application upgrade). When specified, the sudo command will prompt the user to enter the password for the distribution's administrative account (as shown below):


Entering the administrator accounts password allows apt update and apt upgrade to run with escalated permissions (see below):


After entering the administrative password, the user will be prompted if they want to continue (y or n). Entering 'y' continues the apt update and apt upgrade invocation (as follows):


The above image shows the application update and upgrade commands have completed for all applications.

Appendix A: apt Command Documentation

Ubuntu's documentation section, Package Management, contains a handy write-up on the apt command. The apt command options to install, remove, update, and upgrade are documented as follows on the aforementioned documentation link: 



WSL 2.0: Uninstalling a Linux Distribution from Windows Subsystem for Linux (WSL 2.0)

Consider the scenario where Ubuntu version 18.04 was installed on WSL 2.0 and an upgrade is needed to Ubuntu 20.04. This post demonstrates how to unregister (uninstall) a Linux distribution from WSL 2.-0.

1. Open a PowerShell console with administrative access.
2. Invoke using the PowerShell console wsl --list --verbose to list the currently installed Linux distributions.


3. Unregister Ubuntu 18.04 by invoking the following command from the PowerShell console,  wsl --unregister Ubuntu-18.04:




WSL 2.0: Installing Windows Subsystem for Linux (WSL) -- the WSL meant for Docker

Overview

Why install WSL 2.0? Writing scalable backends (web services) using Microsoft's stack is a matter using Docker (or another container technology), .NET Core/.NET 5/.NET 6, and a Linux distribution. The most realistic development platform for a Windows engineer is Windows Subsystem for Linux 2.0 (WSL). To quote Wikipedia's write up on WSL 2.0 (WSL 2), this version of WSL is optimized for Docker/virtualization:

Prerequisites

To quote Microsoft's documentation, "Install WSL", the operating system prerequisites for installing WSL 2.0 are as follows:


The user installing WSL 2.0 should have local administrator rights and PowerShell should be enabled to be executed (see cmdlet Set-ExecutionPolicy). As of this time, PowerShell 7.2 is the most recent version.

Installation Steps

1. Run the PowerShell console window with administrative permissions.
2. From the PowerShell console, invoke wsl --list --online to list the candidate Linux distributions that can be installed.


3. From the PowerShell console, invoke wsl --install and specify Linux distribution to install using -d (e.g. wsl --install -d Ubuntu-20.04 to install Ubuntu 20.04):


4. When invoked wsl --install will launch a new console corresponding to the Linx distribution being installed (see below):


5. Enter the username, and, when prompted, enter and confirm the password. Once these have been specified the screen will appear as follows:


With that WSL 2.0 is installed on Windows.

Friday, January 14, 2022

C#: Using a C# keyword as a variable (a.k.a. verbatim identifiers or @ before a keyword variable name)

C# allows keywords to be used as variable names. When a keyword is prefixed by an @  (such as @class), C# interprets that combination as a "verbatim identifier" which means it is a keyword used as a variable name.

A developer worth their salt will not be naming variables after keywords (such @if, @for, @while, @int, and @static). 

I was tasked with posting to a web service and generated the following C# code using the JSON payload for the web service and the generated code looked as follows (not the real payload):

    public class DepartmentRef
    {
        public string departmentRef { get; set; }
        public string @class { get; set; }
        public string @struct { get; set; }
    }

    public class MessageUpdate
    {
        public List<DepartmentRef> departmentRef { get; set; }
    }

    public class Root
    {
        public MessageUpdate messageUpdate { get; set; }
    }

The code in boldface above shows two verbatim identifiers. When a code generator is used, a verbatim identifier may just be a side effect.