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





Visual Studio Code: Disabling the Minimap

By default, when editing a file, Visual Studio code displays a minimap along the right side of the file being edited:


Visual Studio Code recently added the ability to hide the minimap via the View menu:


The Command Palette can also be used to toggle the minimap using ctrl-shift-P and enter the term minimap:


Appendix A: Visual Studio Code Minimap Documentation

The documentation for the minimap is found at Minimap. The documentation's explanation as to the purpose of the minimap is as follows:




Monday, January 11, 2021

Visual Studio Code: Fixing Error "Your connection is not private" (NET::ERR_CERT_AUTHORITY_INVALID)

 When debugging/running a web service or web application in Visual Studio Code or Visual Studio the following error will show up in the browser the first time an application is run immediately after the .NET 5 (or other) SDK is installed:


The text of the error is: 

Your connection is not private

Attackers might be trying to steal your information from localhost (for example, passwords, messages, or credit cards). Learn more

NET::ERR_CERT_AUTHORITY_INVALID

The error is caused because the certificates used during running/debugging provided by the .NET 5 SDK have not been trusted on the local host. The DotNet CLI provides a command-line for trusting these certificates:

dotnet dev-certs https --trust

The command above should be run from Visual Studio Code's Terminal window:


When the above command is invoked, the following dialog is displayed:


Click on Yes to trust the certificates. Restart the application being debugged/run in Visual Studio code and the error should disappear. 

Sunday, January 10, 2021

Visual Studio Code: Creating a C#/WebApi Project with .Net 5

The DotNet CLI worked for .NET Core and it works for .NET 5. The steps to create a C# web service (WebApi project) are as follows once .NET 5.0 is installed:

From a Visual Studio Code Terminal window invoke a command such as the following which creates a project named Learn: 

dotnet new webapi --name Learn

An example of invoking the following command from from Visual Studio Code's Terminal is as follows:


The dotnet new command created a folder, C:\Blog\DotNet5\Learn. Open that folder from Visual Studio Code (File menu | Open Folder):


Once the folder is open, Visual Studio Code will prompt to create the assets required to build and debug the project:

Select Yes from the dialog.

Visual Studio Code's Explorer shows the files contained in the folder:


The Learn.csproj file is the project file. Double clicking on the file in Explorer displays Learn.csproj in an edit window:


The project file, Learn.csproj, shows that the project type is a WebApi project of the .NET 5/.Net Core variety Microsoft.NET.Sdk.Web. The TargetFramework is net5.0 and the PackakageReferemce elements are NuGet packages used by the project.

The program.cs file is the entry point to the web service, meaning the code initially runs when the service is launched:


The entry point configures the web service and invokes the startup class (see Line 17). The startup class is aptly named Startup and is defined in Startup.cs. The purpose of the Startup class is to setup configuration information and invoke middleware used by the service.

The project contains a sample model, WeatherForecast.cs:


The project also contains a sample controller, WeatherForecast.cs, inside the project folder's Controller's folder:


The controller specifies the routes to be used by the service.

The project folder contains project-level configuration files:

The appsettings.json file contains project-level application settings and appsettings.Development.json contains project-level application settings which are only invoked when the service runs in the development environment. Both of the aforementioned applications settings files initial contain settings related to logging and the allowed hosts. A mature project would contain a application settings file per-environment (production, stage, qa, integration, etc.).

The .vscode folder contains files specific to how the code behaves when run under Visual Studio Code. The .vscode folder contains the files the launch.json and task.json. The task.json file contains tasks including how to build the project using Visual Studio Code:


The command to build in Visual Studio Code is ctrl-shift-b.

The launch.json file under the .vscode folder contains launch settings for when the service is run by Visual Studio Code:


Line 13 of launch.json (above) shows that the DLL invoked on launch is Learn.dll. The settings at Line 23 indicates that when running under Visual Studio Code, the environment is considered to be Development which means that the application settings in appsettings.Development.json takes precedence over appsettings.json.

The Properties folder contains a launchSettings.json file which pertains to when the service is launched outside of Visual Studio Code:


Line 25 shows the HTTPS and HTTP URL used when the web service is launched. The environment, Development, is specified as Line 27. When run under Visual Studio Code this value is ignored in deference to the setting in .vscode\launch.json.

To run/debug the code, from Visual Studio Code, click on the Debug Hub:


From the Debug Hub click on the Start Debugging button:


When the Start Debugging button is clicked (above) a browser tab such as the following will be lauched:


The content in the above browser instance is not overly useful. To display something with meaning, change the URL to: https://localhost:5001/swagger

The above change to the URL displays the following:


To glean a glimpse of what Swagger can provided check out the documentation What Is OpenAPI?:

Appendix A: Dotnet New

The documentation for the DotNet CLI's new command is found at dotnet new. The available project templates described in the documentation are as follows: