Chapter 2

Understanding the .NET Framework

IN THIS CHAPTER

check Getting acquainted with the .NET Framework versions

check Paying attention to what’s new in .NET 4.8

check Viewing the contents of the Global Assembly Cache

check Examining .NET Standard and .NET Core

Many people wonder why they need to understand the .NET Framework and, more important, why it’s bundled with PowerShell. Using .NET, not only can you do things in PowerShell that you wouldn’t be able to do natively, but you also can create functions and modules with the .NET code that you can reuse.

In this chapter, I explain the basics of .NET and how to interact with it. I don’t cover .NET programming — that’s a whole other book’s worth of material.

Introducing the Various Versions of .NET Framework

Before I jump into .NET versions, I want to make sure that you understand what the .NET framework actually is. A framework allows a programmer to call code instead of having to write the code each time the programmer wants the functionality. The .NET framework gives developers the code they need to write .NET applications without having to custom develop every single little piece of code themselves. .NET is integrated with PowerShell, so you can call the same snippets of .NET code that developers can from within PowerShell, either in the console or in a script. For instance, Figure 2-1 shows a piece of .NET code called from PowerShell that displays processes on the system. This piece of .NET code is actually used for the PowerShell Get-Process cmdlet.

Snapshot of the .NET Framework expands the functionality of PowerShell greatly with code that can be called on the console or via script.

FIGURE 2-1: The .NET Framework expands the functionality of PowerShell greatly with code that can be called on the console or via script.

Each new version of the .NET Framework adds new functionality and fixes old problems. The .NET Framework follows a similar cadence to most products in that it has major and minor versions. The major releases tend to focus heavily on new features, while the minor versions add features and fix issues found in the previous releases.

As of the time of this writing, version 4.8 is the current major version and current minor version. The 4.8 version is what is installed by default on Windows Server 2022. It’s very common to have multiple versions of the .NET Framework installed on the same system; there is usually no issue with them co-existing.

Tip The version of .NET you’re currently on is stored in the Windows Registry. To locate the release number, you can use the following command:

(Get-ItemProperty 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDPv4Full' -Name Release).Release

This command returns a number. Table 2-1 lists the minimum number for each major version. When I run this command on my system, for example, I get 528449. According to Table 2-1, that means that I have version 4.8, which is correct because the minimum value for 4.8 is 528040.

TABLE 2-1 .NET Versions with Release Values

Version

Minimum Value

.NET Framework 4.5

378389

.NET Framework 4.5.1

378675

.NET Framework 4.5.2

379893

.NET Framework 4.6

393295

.NET Framework 4.6.1

394254

.NET Framework 4.6.2

394802

.NET Framework 4.7

460798

.NET Framework 4.7.1

461308

.NET Framework 4.7.2

461808

.NET Framework 4.8

528040

Source: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#ps_a

Tip Most of the time, you have one version of .NET installed, and that’s it. Sometimes, though, you may have a legacy application that needs an older version of .NET, and a newer application that requires a newer version of .NET. The great thing about .NET is that you can run more than one version side by side. For instance, your legacy application may need .NET 3.5, but your newer application needs .NET 4.5. You can install both versions of .NET on your system, and each application will be able to run with the version of .NET that it needs.

.NET is available in the server operating system as a feature. To install .NET, follow these steps:

  1. In Server Manager, choose Manage ⇒ Add Roles and Features.
  2. On the Before You Begin screen, click Next.
  3. On the Select Installation Type screen, choose Role-Based or Feature-Based Installation, and then click Next.
  4. On the Select Destination Server screen, click Next.
  5. On the Select Server Roles screen, click Next.
  6. On the Select Features screen, select either .NET Framework 3.5 Features (which installs .NET Framework 2.0, 3.0, and 3.5) or .NET Framework 4.8, and then click Next.
  7. On the Confirm Installation screen, click Install.

Focusing on New Features in .NET 4.8

.NET version 4.8 introduced new features in a few key areas:

  • Base Classes: Cryptography improvements were made when using FIPS mode. You would previously get an error if you used one of the managed versions of a cryptographic provider class that had not been certified for FIPS 140-2.

    Zlib, which is used for compression, has been updated as well to include some issue fixes.

  • Windows Communication Foundation (WCF): A health endpoint can be enabled to allow access to service health information via WCF. This is referred to as ServiceHealthBehavior, and it can be enabled through code or through a configuration file.
  • Windows Presentation Foundation (WPF): WPF is a UI-based framework that aids in the creation of desktop applications. In this updated version of WPF, support has been added for high dots per inch (DPI) configurations. This allows applications that need high DPI to be scaled properly so that they’re crisp and clear to the end user.
  • Common Language Runtime: Updates were made to the Just-In-Time (JIT) compiler. It’s now based on the .NET Core 2.1 version of the JIT compiler.

    Tip If you’re wondering why they’re using something so old, well, they aren’t. .NET has historically been specific to Windows, and .NET Core is an open-source software framework that allows you to create .NET applications on Windows, Linux, and macOS.

    Some improvements were also made to how memory is managed with the Native Image Generator (NGEN) and malware scanning capabilities, which ensure that all assemblies are scanned, not just those loaded from disk.

Viewing the Global Assembly Cache

Before I dive into the Global Assembly Cache (GAC), you may be wondering what it is. The GAC is responsible for storing assemblies that are shared by multiple applications on a computer. The assembly, at its most basic definition, is an executable of some kind. It contains all the code that will be run and serves as the boundary for the application. Many assemblies are installed when .NET is installed on your system.

.NET Framework versions 4 and up store their assemblies in %windir%Microsoft.NETassembly. The %windir% is a placeholder for the Windows directory, which is typically located at C:Windows.

Viewing the assemblies in the GAC is done using a tool called gacutil.exe. This tool is a part of the Developer Command Prompt for Visual Studio so you need to install Visual Studio on your system if you want to play with the GAC on your system. Visual Studio IDE Community is the free version and does include the Developer Command Prompt. Figure 2-2 gives you an idea of what the GAC looks like when you view the assemblies. I've typed the following command:

gacutil.exe -l

This command lists all the assemblies within the GAC.

Snapshot of viewing the contents of the GAC with the Developer Command Prompt.

FIGURE 2-2: You can view the contents of the GAC with the Developer Command Prompt.

Understanding assembly security

Because the GAC lives in the Windows folder, it inherits the permissions of the Windows folder. In many cases, you may want to tighten the permissions on the GAC directories so that only administrators can delete assemblies. If someone deletes an assembly that the system or an application relies on to function properly, that application will no longer work.

Identifying the two types of assembly privacy

Two types of assemblies make up the .NET presence on your system:

  • Private: Private assemblies are deployed with an application and can only be used by that application. Think of them like the children on the playground who won’t share.
  • Shared: Shared assemblies are available to be used by multiple applications on your system. They’re stored in the WinSXS folder and are installed via Windows Update and Windows Installer packages.

Viewing assembly properties

In older versions of the Windows Server operating system, you could simply right-click an assembly to get all the properties of the file. That function was removed several operating system versions ago. Now if you need to get information on the assembly file, your best bet will be to go through PowerShell. Say that I want to view the version information on the accessibility.dll that is in use. This is the command that I would need to run:

[Reflection.AssemblyName]::GetAssemblyName('C:WindowsMicrosoft.NETFrameworkv4.0.30319accessibility.dll').Version

After I've run this command I’m presented with the major, minor, build, and revision numbers, as shown in Figure 2-3.

Snapshot of using PowerShell to view the assembly properties in Windows Server 2022.

FIGURE 2-3: You need to use PowerShell to view the assembly properties in Windows Server 2022.

Understanding .NET Standard and .NET Core

The .NET Framework has been a staple for many years, but newer frameworks are gaining in popularity.

.NET Core

.NET Core is one of the newest members of the .NET Framework family. It’s open source and it can be run on Windows, Linux, and macOS. With .NET Core, you can build applications that are cross-platform. If your application is developed with .NET Core, then only .NET Core applications will be compatible — you won’t be able to support Xamarin or the classic .NET Framework. .NET Core is an implementation of the specifications that are set in the .NET Standard.

You may wonder why you would use .NET Core if it isn’t compatible with the other runtimes. There are a couple good reasons:

  • You can develop .NET Core on Windows, Linux, or macOS.
  • If you’re coding for a mobile application, .NET Core is optimized for mobile work.

.NET Standard

.NET Standard is a set of APIs that all the .NET frameworks must support. This includes .NET Core, Xamarin, and the classic .NET Framework. It’s important to note that .NET Standard is a specification, not a framework. It’s used to build libraries that can be used across all your .NET implementations, including the traditional .NET Framework, the newer .NET Core, and Xamarin.

Tying it all together: .NET and PowerShell

PowerShell Core 6.0 uses the newer .NET Core as its runtime. This means that you can now run PowerShell on Windows, Linux, and macOS. PowerShell Core also enables you to take advantage of all the awesome .NET Core APIs in your commands and scripts, which really extends the utility and capabilities of your scripts. You can start working with PowerShell Core without impacting your current installation of PowerShell because both PowerShell and PowerShell Core can be run side-by-side. PowerShell Core is available for download from the PowerShell repo on GitHub at https://github.com/PowerShell/PowerShell/releases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.136.168