Friday, January 22, 2021

Azure: Selecting a Virtual Machine Capable of Running Docker

In the post, Configuring VMWare Fusion to Support Docker Running on the Guest OS, it was pointed out that nested virtualization consumed a significant number of resources. Windows 10 running under VMWare Fusion on a MacBook Pro 2017 with an I7 and 16 GB of RAM was unusable when a Docker container was run on the Guest OS. One way to avoid purchasing a new computer is to create a virtual machine in Azure or AWS that is capable of Docker development. Microsoft provides a $150 per-month Azure credit with an MSDN subscription which makes an extremely powerful virtual machine cost nothing provided the VM is deallocated when not in use. 

Rather naively, a B Series Azure VM was created. The Docker Desktop crashed with an error when run on this virtual machine. The error stated that virtualization was not supported and the BIOS should be modified to support virtualization. As it turns out, not all VM series in Azure support nested virtualization. The innocuous blog post, Nested Virtualization in Azure, on Microsoft's Azure site identifies that nested virtualization is supported by Dv3 and Ev3 series virtual machines. Later versions than V3 of the D and E series virtual machines also support nested virtualization.

A VM was created using a Standard D-Series V4 (8 vcpus, 32 GiB memory) virtual machine at a cost of $373.03 per-month in West US. By deallocating the VM when not in use it can be run eight hours a day, seven days a week without exceeding the $150 per-month free credit. Docker ran without issue on the D-Series V4 (8 vcpus, 32 GiB memory) virtual machine.

Appendix A: Azure Virtual Machine Series

Azure's documentation on virtual machines, Virtual Machine series, presents a comprehensive overview of the virtual machine series (categories). Excerpts from this documentation are as follows:



























Wednesday, January 20, 2021

Configuring VMWare Fusion to Support Docker Running on the Guest OS

My personal Windows device is a Windows 10 guest OS that is hosted on a MacBook Pro running OS/X with VMWare Fusion. After installing Docker (Install Docker Desktop on Windows) I received numerous errors that the BIOS was not configured to support virtualization.

To enable virtualization the VM's Settings | Processors and Memory need to be modified. In VMWare Fusion the settings for a VM can be found by right clicking the virtual machine (the VM's name is W10Core3 which is now a .NET 5 VM) and selecting Settings from the context menu:


The Settings dialog (see below) contains Systems Settings group in which there is a Processors & Memory icon:


Clicking on the Processors & Memory icon displays the Processors & Memory dialog (see below). Expand Advanced options at the bottom of the dialog:


From the Advanced options section check the Enable hypervision applications in this virtual machine checkbox (see below):

Enable hypervision applications in this virtual machine is not a Windows 10 specific setting. The Guest OS running Docker would be Linux, Windows 10 or OS/X.

The MacBook Pro running VMWare Fusion with the Windows 10 guest OS is configured as follows:


When Docker is running, OS/X is running Windows 10 via VMWare Fusion and the Windows 10 is running WSL 2 which is Linux virtualization. This is nested virtualization and running Docker makes Windows 10 nearly unusable and subject to application crashes.

My solution to this was to create an Azure VM using my $150 per-month Azure credit received through my MSDN subscription but the details of this are a future post. 

Friday, January 15, 2021

C# Nominal versus Positional Object Creation

C# 9.0 introduces positional record types. Before discussing this language feature, a review is needed of the different styles of object creation in C#:

  • Positional: the properties of a class that require initialization are passed as parameters to a constructor. The constructor initializes the properties by assigning parameters to properties.
  • Nominal: the constructor for a class takes no parameters or fewer parameters than the number of properties that require initialization. When the object is constructed the properties are assigned using an object initializer.

To clarify the terms positional, nominal and object initializer consider the following class used to represent a technical certification: 

An example of positional object creation is as follows:


Line 30 shows positional object creation and line 33 shows positional object creation C# 9.0 fit and finish.

An example of nominal object creation is as follows where object initializes used to assign the Name and Description properties:


Line 30 shows nominal object creation and line 33 shows nominal object creation C# 9.0 fit and finish.





 

Visual Studio Code: Disabling Code References on Windows (a.k.a. Disabling CodeLens)

When including code snippets in documentation or a PowerPoint presentation it is desirable to remove N References displayed by default in a Visual Studio Code window. The code below shows 0 References to the class Certification (line 3), 1 reference to the Name property (line 15), and 1 reference to the Description property (line 17):


The reference information is provided by CodeLens and after disabling CodeLens, the code window appears as follows:


To disable CodeLens invoke the File Menu's Preferences menu item and select Settings (File | Preferences | Settings): 

From the User page, select the Text Editor tab and scroll until the Code Lens checkbox is visible: 


Uncheck the Code Lens checkbox.

An excellent write up on CodeLens can be found at Extensions using CodeLens.

C# 9.0: Fit and Finish

In some respects C# 3.0's var keyword is the foil to C# 9.0's fit and finish feature. Microsoft's overview of C# 9.0 features (What's new in C# 9.0) defines fit and finish as:

With the var keyword, the type of a variable is known based on the type assigned such as the following where the type is DateTime:

var lastTime = new DateTime(2020, 13, 31, 23, 59, 59);

C# 9.0's fit and finish removes the need for the type associated with the new keyword as the declaration makes this known:

DateTime valentinesDay = new(2021, 2, 14, 12, 0, 0);

Fit and finish can be used in field initialization (see line 11, 13, and 15):


Fit and finish can be used in conjunction with init only setters. To demonstrate consider the following class:


An instance of the Employee class can be instantiated as follows using fit and finish:

Employee employee = new() { Name = "Joe", Salary = 100000 };


Thursday, January 14, 2021

C# 9.0: Init Only Setters

 C# 9.0 introduces init only setters which allow properties to be assigned when an object is constructed and after construction those properties are read-only. To understand how init only setters are beneficial consider the Employee class:


The Salary property (line 9) has a private setter which means to initialize this property a value must be passed to the constructor (line 5). Imagine a case where the Employee class had thirty properties with private setters. The constructor in this case would contain an unmanageable number of parameters. 

An example of an init only setter is Employee class' Salary property (line 18):


The Salary can be set at initialization such as:

var employee = new Employee { Name = "Joe", Salary = 100000 };

The Name property is public and can be assigned after initialization. The Salary property uses an init only setter which means it cannot be assigned after initiation.

Wednesday, January 13, 2021

Visual Studio Code: More Efficient Building of a WebService Project

A .NET 5 WebApi project was create in post Visual Studio Code: Creating a C#/WebApi Project with .Net 5. Building a Visual Studio Code project such as the previous example is actually a bit tedious and it does not have to be. This post will show how building a .NET 5 Web Api project in Visual Studio Code behaves by default and how it can be made more efficient.

To build the project we previously created, select Terminal | Run Build Task (a.k.a. CTRL-SHIFT-B):


When Terminal | Run Build Task the Command Palette is displayed with the build command provided in the dropdown:


Running build from the Command Palette builds the project. It would make more sense if invoking Terminal | Run Build Task simply builds the project. To achieve this, open the .VSCode folder's tasks.json file:


Under line 14 shown above the following text needs to be added as it identifies the default build task;

"group": {
        "kind": "build",
        "isDefault": true
      }

The updated launch.json file looks as follows with the new text added as lines 15 to 18:


Following the addition of the default build task, invoking Terminal | Run Build Task directly builds the project without the middle step of using the Command Palette. CTRL-SHIFT-B will also directly invoke the build.