Monday, October 24, 2022

Documentation System.Net.WebClient

While working with some legacy code, I needed to read Microsoft's documentation associated with WebClient (see WebClient Class). I found ten issues with the documentation's code sample for WebClient and fixed them.

As a disclaimer, Microsoft's documentation, WebClient Constructor, specifies that WebClient is obsolete (yes, I will even fix the documentation for obsolete classes):


I found seven instances where the WebClient class showed examples of events being subscribed a handler method that was not included in the documentation (all changes to the source were merged into main):



Saturday, October 22, 2022

PowerShell: Installing the latest version of PowerShell on a Windows Client Host

Overview

This post will demonstrate how to install the latest version of PowerShell on a Windows client host. Before installing the latest version of PowerShell it is important to recognize that a given host can have multiple versions of PowerShell installed. It is possible to have PowerShell 5.x and 7.x running on the same host and in fact, this can be quite handy for certain development situations (backward compatibility). After installing the latest version of PowerShell, it will also be shown how to run the previous version of PowerShell (5.x) or the latest version of PowerShell (7.x)

Installing PowerShell

The Microsoft recommended way to install Powershell on Windows 10 and Windows 11 clients is using winget. Microsoft's documentation, Installing PowerShell on Windows, points out that winget is not available on Windows server variants:


To install the latest version of PowerShell: 

1. Launch a PowerShell console window with administrative privileges. 

2. Before installing the latest version of PowerShell, displays the $host variable's Version property using the PowerShell console window.

Notice above that the version of PowerShell installed is 5.1.608 which is not the latest as 7x. is the latest.

3. To determine the latest version of PowerShell available for installation, from the PowerShell console invoke, winget search, as follows:

winget search Microsoft.PowerShell --source winget

Invoking the above command displays the following where both the latest stable version and the latest preview version of PowerShell are displayed:


4. To install the latest stable version of PowerShell, invoke winget with the following options:

winget install --id Microsoft.Powershell --source winget

Invoking the above command in a PowerShell console window displays the following:


Installing PowerShell 7.x will uninstall PowerShell 6.x. PowerShell 5.x can be installed on the same host as PowerShell 6.x and PowerShell 5.x can be installed on the same host as PowerShell 7.x.

Running the Latest Version of PowerShell

After installing the latest version of PowerShell, display $host.Version in the PowerShell console window one more time:



Notice in the screenshot above that the version of PowerShell running in the console window is 5.1 but the latest version, PowerShell 7.2.6 was installed.  Microsoft documentation, Migrating from Windows PowerShell 5.1 to PowerShell 7, points out that each version of PowerShell is installed in a different location:

To run a PowerShell console, the installed folder is not required.

To run PowerShell 5.x invoke:
  powershell.exe

To run PowerShell 6.x or 7.x invoke:
  pwsh.exe

Running a PowerShell 7.x Console

To run a PowerShell 7.x console window:
  • Click on the Windows key and type pwsh into the search textbox (see below)


  • Select "Run as administrator" to invoke a PowerShell 7.x console window running with administrative priveledges: 

Notice in the previous screenshot that the PowerShell console has identified that a new version of PowerShell is available. It is important to note that winget may be delayed by a few days before receiving the latest version of PowerShell. This post was written on October 22, 2022 and PowerShell 7.2.7 was built on October 17, 2022.

Getting the latest PowerShell MSI

Microsoft recommends winget for installing PowerShell (see: Installing PowerShell on Windows). As PowerShell is open source, Microsoft placed the code for PowerShell on Github, PowerShell. The page in Github containing all installable releases of PowerShell (as downloadable MSI files). The installers on Github include the latest release is PowerShell releases:


The screenshot above shows that PowerShell 7.2.7 was made available on Github on October 20, 2022, hence winget still considers PowerShell 7.2.6 as of October 22, 2022.

Visual Studio Code

At some point, a Microsoft tool will prompt you to upgrade to the latest version of PowerShell. Below is a dialog displayed by Visual Studio Code will editing a PowerShell file (*.ps1):


The prompt was seen on October 23, 2022, six days after the release was built (October 17, 2022).

Installing PowerShell Preview Version

If the latest preview version of PowerShell, invoke the following from a PowerShell console window running with administrative permissions:

winget install --id Microsoft.Powershell.Preview --source winget


Friday, October 21, 2022

Updating XML documention: XML schema (XSD) validation with XmlSchemaSet

In my never-ending quest to keep Microsoft's documentation, I noted the following in XML schema (XSD) validation with XmlSchemaSet:


The code above subscribes to an event by creating a new delegate instance (new ValidationEventHandler) which was how code was written in for C# 1.0 to 1.1 (2002-2004):

  booksSettings.ValidationEventHandler +=
    new ValidationEventHandler(
      booksSettingsValidationEventHandler);

As of C# 2.0 (released in 2005) there is no need to explicitly create a delegate instance when subscribing to an event:

  booksSettings.ValidationEventHandler +=
    booksSettingsValidationEventHandler;

The updated code is as follows:

I created a fork in Microsoft's documentation (open source on Github.com) and within twenty-four hours Microsoft accepted the change and merged the code:









Saturday, October 15, 2022

C#: Twitter Volume Stream Coding Challenge (Getting Started cURL/Postman)

Overview

I accepted a C# coding challenge targeting Twitter's Volume Streams API (a RESTful web service). This post demonstrates:

  • Requirements of the coding challenge
  • How to create a Twitter account and a Twitter developer account
    • This includes acquiring a bearer token facilitating access to the Twitter API
  • Using cUrl and Postman (online) to access Twitter's API
Future posts will demonstrate how to implement the coding challenge (C#/.NET 7).

Requirements

The application developed with regard to the coding challenge should generate the following analytics:

  • total number of Twitter tweets
  • top ten hashtags

Setup

The steps to access Twitter's API are well documented but the basic steps are:

  • Create a Twitter account: for readers (like me) who did not have a Twitter account, you can figure it out
  • Create a developer account: Developer Account - Twitter Developer
  • As part of creating a developer account, Twitter also requires an application to be created and for the application Twitter provides:
    • API Key
    • API Key Secret
    • Bearer Token

The speicfic Twitter API V2 to access for this challenge is Volume Streams:


Sample Code

C#

The sample code for the Twitter API v2 is on  GitHub at https://github.com/twitterdev/Twitter-API-v2-sample-code. Cloning the previous git repo reveals zero samples with a CS (C#) file extension. There are Python, Java, and JavaScript samples but not C#.

cURL

To use the Volume Streams API using cURL on Ubuntu first create an environment variable named APP_ACCESS_TOKEN:

export APP_ACCESS_TOKEN=<bearer token provided by Twitter here>

Once the APP_ACCESS_TOKEN environment variable is created, cURL can be used to invoke the Volume Streams API from a terminal window:


When the above cURL command is invoked the output is as follows (a constant stream of 1% of the tweets):

Postman

Before accessing the Twiter Volume Stream endpoint using Postman, create a Postman account. This is required to use Postman for the Web. Legacy Postman was a desktop application but in September 2020 Postman for the Web (open Beta, July 16, 2020) was released for General Availability.

To invoke Twitter's Volume Stream endpoint using Postman, navigate to the "Run in Postman" button and click:


Clicking on Run in Postman invokes the following URL, https://t.co/twitter-api-postman. The previous URL launches Twitter's Public Workspace for Postman: 


Expand the workspaces folders to expose Twitter API V2 / Sample Stream as follows:


The selected item displays GET used to invoke the volume stream endpoint:


Developers who have used Postman will instinctively click on the Authorization tab in order to enter the Twitter-provided bearer token so that the GET can be invoked using the Send button:


Entering the bearer token and clicking on Send will:
  • Require the user to log in to Postman
  • Require the user to create a fork: Twitter's Public Workspace for Postman is a Git repo. Each user runs their own fork and this allows users to contribute to the project. This also isolates a user's instance.
  • Require the user to create a Postman workspace
Once the above tasks are completed the bearer token can be added under the Authorization tab and the endpoint can be invoked using the Send button:

Connection Limits

The developer account type Twitter offers is limited to one connection at a time to the Volume Stream web service. Running the cURL sample above and trying to invoke Postman against the Volume Steam endpoint expectedly generates a 429 error (too many requests):


Appendix A: Coding Challenge