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 Create

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 (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.

Tuesday, January 12, 2021

Visual Studio Code: .NET 5.0 Reusing the Current Browser Tab for Debugging

By default, each time a web service is run/debugged in Visual Studio Code, a new browser windows is opened.  Running the web service project Visual Studio Code: Creating a C#/WebApi Project with DotNet 5 three times launches (by default) a browser and opens three tabs (once per debug session):

For a Visual Studio Code web service project to not open a browser window each time the web service is debugged, the launch.json file under the .vscode folder needs to be modified. An example of the deafult launch.json is as follows:

To not open a browser window comment out the serverReadyAction attribute (lines 18-21).